Hello everyone!
I want to make client-sided collecting system but each time remote event for reward fires it makes bigger and bigger lag spike and i don’t understand why
Client:
local touchedCubes = {}
local function onTouch(cube, cubeData)
if touchedCubes[cube] then return end
if not cube:IsA("BasePart") then return end
local connection
connection = cube.Touched:Connect(function(hit)
touchedCubes[cube] = true
AnimateCollect(cube)
Remotes.CubeCollected:FireServer(hit, cubeData)
game.Debris:AddItem(cube, 0.6)
spawn(function()
task.wait(0.5)
touchedCubes[cube] = nil
end)
if connection then
connection:Disconnect()
connection = nil
end
end)
end
local function spawnCube()
local cubeData = Remotes.CubeData:InvokeServer()
if not cubeData then return end
local cubeTemplate = CubesFolder:FindFirstChild(cubeData.Name)
if not cubeTemplate then return end
local cube = cubeTemplate:Clone()
cube.Position = spawnArea.Position + Vector3.new(math.random(-spawnArea.Size.X/2 + 0.5, spawnArea.Size.X/2 - 0.5), spawnArea.Size.Y/2 + 0.5, math.random(-spawnArea.Size.Z/2 + 0.5, spawnArea.Size.Z/2 - 0.5))
local x, y, z = string.match(cubeData.Value, "([^,]+), ([^,]+), ([^,]+)")
if x and y and z then
cube.Position = Vector3.new(tonumber(x), tonumber(y), tonumber(z))
onTouch(cube, cubeData)
cube.Parent = workspace:FindFirstChild(player.Name.."_Cubes")
end
end
while playerCubesFolder do
spawnCube()
end
Server:
remotes.CubeCollected.OnServerEvent:Connect(function(player, hit: BasePart, cubeData)
local data = RS.Data:FindFirstChild(player.Name)
cubeData:Destroy()
data.Stats.Cubes.Value += CubesModule[cubeData.Name].Value
data["Total Stats"]["Total Cubes"].Value += CubesModule[cubeData.Name].Value
data.Stats.XP.Value += CubesModule[cubeData.Name].Value/10
end)
local function parseVector3(str)
local x, y, z = string.match(str, "([^,]+), ([^,]+), ([^,]+)")
if x and y and z then
return Vector3.new(tonumber(x), tonumber(y), tonumber(z))
end
return nil
end
local function isPositionTaken(pos, takenPositions, tolerance)
for _, taken in takenPositions do
if (pos - taken).Magnitude <= tolerance then
return true
end
end
return false
end
local spawnDebounces = {}
remotes.CubeData.OnServerInvoke = function(player)
print("function")
local data = RS.Data:WaitForChild(player.Name)
local spawnSpeed = data["Cube Stats"].Speed.Value
local spawnLimit = data["Cube Stats"].Limit.Value
if spawnDebounces[player.Name] or #data.Cubes:GetChildren() >= spawnLimit then return nil end
spawnDebounces[player.Name] = true
spawn(function()
task.wait(1/spawnSpeed - 0.001)
spawnDebounces[player.Name] = nil
end)
local chosedCube = CubesModule.ChooseCube()
local cubeData = Instance.new("StringValue")
cubeData.Name = chosedCube
local randomX = math.random(-spawnArea.Size.X/2 + 0.5, spawnArea.Size.X/2 - 0.5)
local randomZ = math.random(-spawnArea.Size.Z/2 + 0.5, spawnArea.Size.Z/2 - 0.5)
local pos = spawnArea.Position + Vector3.new(randomX, spawnArea.Size.Y/2 + 0.5, randomZ)
cubeData.Value = tostring(pos)
cubeData.Parent = data.Cubes
return cubeData
end
as you can see in the video lag spike is bigger and bigger with each cube touched (event fire)
without event for reward there is no lags but ofcourse i need to give players a reward any ideas on how to fix that?
making loop slower also doesn’t change anything and cubes are being collected from distance because i also want to include collect radius in the game which is already here created on different server script
pls do not accent on anything else only on what causing lag spike i’m just stuck because of that lag spike
i’ll appreciate any help!
