Search code examples
luaroblox

how to fix server lag in roblox


I am making this clicker game on Roblox. Everything is working fine except that when there are multiple people playing there is a lot of server lag and sometimes the clicking doesn't register.

When the button is clicked, it fires an event:

local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
local button = script.Parent

button.MouseButton1Click:Connect(function()

    game.Workspace.GetClickScript.GetClick:FireServer()

end)

And a server script detects the event fired and does this:

local debounce = false
script.GetClick.OnServerEvent:Connect(function(plr)
    if not debounce then
        debounce = true
        local leaderstats = plr:WaitForChild("leaderstats")
        leaderstats.Clicks.Value += 1
        wait(0.2)
        debounce = false
    end
end)

Except it gets extremely slow when multiple players are clicking the button (because it is a server script). Is there a way to make it faster, or even make it a local script?


Solution

  • Firstly, your code looks alright. The problem is with your use of debounce, debounce is never really used in this way. Every time one player fires the event, debounce stops other players from changing their cash for 0.2 seconds.

    I would remove debounce entirely, tho this shouldn't be causing problems to the magnitude you are experiencing. Try this without debounce and let me know if it works?

    If it doesn't you've most likely got a virus in your game, or just bad wifi on the clients.