Help me with my unit mover please!11!1!

It errors at line 44: attempt to index nil with number

Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local replicatedModuleScripts = ReplicatedStorage.ReplicatedModuleScripts
local UnitClassStats = require(replicatedModuleScripts.UnitClassStats)

local RunSerivce = game:GetService("RunService")

local actor = script:GetActor()
if actor == nil then
	local remoteEvents = ReplicatedStorage.RemoteEvents
	local spawnUnit = remoteEvents.SpawnUnit
	local moveUnit = remoteEvents.MoveUnit
	
	_G.Units = {}
	
	local Workers = {}
	for workerIndex = 1, 32, 1 do
		local newActor = Instance.new("Actor")
		script:Clone().Parent = newActor
		table.insert(Workers, newActor)
	end
	
	for _, worker in Workers do
		worker.Parent = script
	end
	
	local Random = Random.new()

	spawnUnit.OnServerEvent:Connect(function(player, position, class)
		_G.Units[#_G.Units + 1] = {
			["Position"] = position,
			["Class"] = class
		}
	end)

	moveUnit.OnServerEvent:Connect(function(player, index, startPosition, endPosition)
		_G.Units[index].Postition = startPosition
		Workers[Random:NextInteger(1, #Workers)]:SendMessage("MoveUnit", index, startPosition, endPosition)
	end)
	
	return
end

actor:BindToMessageParallel("MoveUnit", function(index, startPosition, endPosition)
	local lerpTime = (startPosition - endPosition).Magnitude / UnitClassStats[_G.Units[index].Class].Speed
	local lerpStartTime = tick()
	local lerpRunningTime = 0
	local lerp
	lerp = RunSerivce.Heartbeat:Connect(function(deltaTime)
		lerpRunningTime += deltaTime
		local alpha = math.clamp(lerpRunningTime/ lerpTime, 0, 1)
		_G.Units[index].Position = startPosition:Lerp(endPosition, alpha)
		if alpha >= 1 then
			lerp:Disconnect()
		end
	end)
end)

And line 44 is…?

local lerpTime = (startPosition - endPosition).Magnitude / UnitClassStats[_G.Units[index].Class].Speed

You didn’t close your brackets.
UnitClassStats[_G.Units][index][Class].Speed

Also, keep in mind you misspelled ‘Position’ here

1 Like

You misspelled Position on line 36 as well.

The brackets are correct but thanks for the tip of the miss spelling.

instead of lerpTime try replacing it by 1

1 Like

Thanks for your reply, it helped me alot!