i have a local script in a gui which is basically responsible for selling resources.
it uses lots of remote functions and events to change values, destroy instances and all that stuff, and its very laggy. here is the script and a demonstration of how laggy it is:
selling 50 planks:
local plr = game.Players.LocalPlayer
local yes = true
script.Parent.MouseButton1Click:Connect(function()
if yes == true then
yes = false
if script.Parent.Parent.ItemName.Value == "Deska" then
local functions = require(game.StarterGui.LocalFunctions)
local iteminfo = game.ReplicatedStorage.Remotes.GetTreeInfo:InvokeServer(script.Parent.Parent.Type.Value)
for i = 1,script.Parent.Parent.SellAmount.Value do
for i,v in pairs(plr.Inventory.PlanksAndRocks:GetChildren()) do
if v.ToSave.Type.Value == script.Parent.Parent.Type.Value then
if v.ToSave.Amount.Value > 0 then
game.ReplicatedStorage.Remotes.ChangeValue2:InvokeServer(v.ToSave.Amount,v.ToSave.Amount.Value-1)
script.Parent.Parent.Parent.Items:FindFirstChild(v.ToSave.Type.Value).Amount.Amount.Value -= 1
if v.ToSave.Amount.Value <= 0 then
game.ReplicatedStorage.Remotes.DestroyInstance:InvokeServer(v)
end
game.ReplicatedStorage.Remotes.ChangeValue2:InvokeServer(plr.leaderstats.Cash,plr.leaderstats.Cash.Value+iteminfo.plankmoney)
break
else
game.ReplicatedStorage.Remotes.DestroyInstance:InvokeServer(v)
end
end
end
end
functions.RefreshSell(plr)
end
wait(0.4)
yes = true
end
end)
A quick google tells me: InvokeServer . Yields … When called, it will pause the Lua thread that called the function until a result is ready to be returned…
So, maybe you should look in those invoked functions for waits/delays
adding onto this, I believe that instead of using InvokeServer, you should use FireServer(), since it doesn’t seem that the client needs to have a response from these requests.
However, it is not very safe to place an event that allows you to change money in the game or destroy objects, in the absence of checks the cheaters could benefit from it.
You shouldnt change any (confidential) values on the client in the first place, instead, fire an event to the server which stores all player information, eg. amount of planks, and then calculate how much money to add for each plank
The way you approach this is instead of firing an event to the server with information like amount, plank price, you only send what the player is selling, and the server handles the rest.
You need to track the amount of planks on the SERVER, not the client, same thing with the plank selling prices, because exploiters can easily change that. You only use local scripts for visuals and triggers, such as the player wanting to sell logs, dont use them for actually increasing the player’s money or something like that.