How could i make my script "faster?"

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:
sell

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)

Change this to

0.01

for it to be faster.

1 Like

no thats not the problem. the problem is the loop
have tried that

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

1 Like

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.

yeah, but i have to use invoke server because it was glitchy and allowed to get more money than you should

the only thing i could actually replace is destroy instance. ill try that

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.

it would be better if everything was server side

yea that is dangerous but what else can i do to change a value from a local script

Fire an event to the server and wait for a response

wait what do you mean by that?

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

I have no idea what your system looks like so it might be really confusing, but basically, dont change information like money on the client

do you mean like put the entire script into a remote

1 Like

No, the client would just send a “Sell planks” trigger to the server, and the server handles the money

You can try to make something like:

sellEvent:FireServer("planks", 50) -- object and count

then execute the code server sided, check if he owns 50 planks and then give money

im confused

[placeholder because the reply is too short to post]

If youre sending the amount exploiters could easily change that and get infinite money

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.

that will be a lot of work but thanks i guess