Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

lua - 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?

question from:https://stackoverflow.com/questions/65890141/how-to-fix-server-lag-in-roblox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...