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)