ServerScriptService.UnitMove:8: invalid argument #2 (string expected, got number)

(Following tutorial from here)
@McDmnks if you can help debug it would be appreciated

I’m getting the error in the title, in the UnitMove script in ServerScriptService. I’m not sure why this is happening. If anyone can help me fix the error it will be appreciated!

Explorer:
explorerimg

UnitMove: (firing event at line 8)

local positions = {}

game.ReplicatedStorage.Remotes.MoveUnit.OnServerEvent:Connect(function(player, hitpos, unit)
	local loop = true
	
	positions[unit] = (hitpos)
	
	game.ReplicatedStorage.Remotes.CreateBeam:FireClient(player, positions[unit][1], unit, false)
	
	while loop and unit.Parent and positions[unit][1] == hitpos do
		unit.PrimaryPart.Vel.PlaneVelocity = Vector2.new(hitpos.X - unit.PrimaryPart.Position.X, hitpos.Z - unit.PrimaryPart.Position.Z).Unit * unit:GetAttribute("Speed")
		
		if (unit.PrimaryPart.Position - positions[unit][1]).Magnitude > 1 and unit.Parent and positions[unit][1] == hitpos then
			unit:SetAttribute("repeating", true)
		else
			loop = false
			unit:SetAttribute("repeating", false)
		end
		task.wait(0.1)
	end
	
	if unit.Parent then
		if unit:GetAttribute("repeating") == false then
			unit.PrimaryPart.Vel.PlaneVelocity = Vector2.new(0, 0)
			game.ReplicatedStorage.Remotes.CreateBeam:FireClient(player, positions[unit][1], unit, true)
		end
	end
end)

UnitMoveHelper:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	for i,v in pairs(game.Workspace.Units:GetChildren()) do
		if v:GetAttribute("Selected") == true then
			local hit = Vector3.new(mouse.Hit.Position.X, mouse.Hit.Position.Y, mouse.Hit.Position.Z)
			game.ReplicatedStorage.Remotes.MoveUnit:FireServer(hit, v)
		end
	end
end)
1 Like

What arey ou trying to pass exactly? A string, number or a Vector?

It’s because you didn’t copy the code correctly.

You put:

Instead of:

positions[unit] = {hitpos}

This causes problems later, because you do positions[unit][1], which returns nil in your current code because positions[unit] isn’t even a table.