I'm creating a tower defense game and for some reason the defense argument I'm sending to the remote event to place the defence is nil.
Local script under a button:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local button = script.Parent
local mouseDown = false
button.MouseButton1Up:Connect(function() -- when the player presses the button to add a defence
button.Parent.Tip.Visible = true -- a text label
repeat task.wait() until mouseDown -- waits until the player clicks an area on the map
local defense = game.ReplicatedStorage.Defenses.Defense:Clone() -- sets a variable to defense instance
game.ReplicatedStorage.Events.AddDefence:FireServer(defense, mouse.Hit.Position) -- fires the remote that should build the defence
print(defense.Name) -- prints defence and not nil
button.Parent.Tip.Visible = false -- hides the text label
end)
mouse.Button1Up:Connect(function() mouseDown = false end)
mouse.Button1Down:Connect(function() mouseDown = true end)
Script in server script storage:
local events = game.ReplicatedStorage.Events
events.AddDefence.OnServerEvent:Connect(function(_, defense, mousePos) -- when the event fires
print(_, defense, mousePos) -- printed my username, nil and the mouse position
defense.Parent = workspace.Defenses -- supposed to parent the defence to a folder in workspace (doesn't work since the defence is nil)
defense.Position = Vector3.new(math.floor(mousePos.X/8+0.5)*8, 6, math.floor(mousePos.Z/8+0.5)*8)-- supposed to position the defence (doesn't work again)
end)