The value sometimes becomes negative because the script sometimes thinks that the player value is more than it really is, but the product is bought

--remote events in a local script

wait(2)
script.Parent.RemoteEvent.OnServerEvent:Connect(function(player)
	player.leaderstats.Yums.Value = player.leaderstats.Yums.Value - 1000
end)

script.Parent.RemoteEvent2.OnServerEvent:Connect(function(player)
	player.leaderstats.burger.Value = 5
end)

script.Parent.RemoteEvent3.OnServerEvent:Connect(function(player)
	for _, tool in ipairs(player.Backpack:GetChildren()) do
		if tool:IsA("Tool") then
			tool:Destroy()
		end
end
	if player.Character:FindFirstChildOfClass("Tool") then
		player.Character:FindFirstChildOfClass("Tool"):Destroy()
end 
	local backpack = player:WaitForChild("Backpack")
	local eynew = game.ReplicatedStorage.Tools:WaitForChild("burger5")
	print(player.Name)
	wait(0.1)
	eynew:Clone().Parent = backpack
	print("cloned new tool")
 end)

--the function in a script

wait(2)
local player = game.Players.LocalPlayer
local Yums = player.leaderstats.Yums
local Burger = player.leaderstats.burger


script.Parent.MouseButton1Click:Connect(function(player)
	if Burger.Value >= 5 then 
		script.Parent.Text = "already owned"
		wait(1)
		script.Parent.Text = "cost: 1000 Yums"		
	elseif Yums.Value >= 1000 and Burger.Value <= 5 then
		script.Parent.RemoteEvent:FireServer()
		script.Parent.RemoteEvent2:FireServer()
		script.Parent.RemoteEvent3:FireServer()
	else
		script.Parent.Text = "Not enough Yums"
		wait(1)
		script.Parent.Text = "cost: 1000 Yums"
	end
	
end)
1 Like

A few notes -

1.Use task.wait() instead of wait() - it’s more efficient, more accurate and way better.

2.If you’re trying to change the player’s burgers, don’t set their value to 5, but add it to their existent value

3.In your local script - since scripts load before the characters are added, it cant recognize yet ‘leaderstats’.
Do:

local Player = game.Players.LocalPlayer
local Yums = Player:WaitForChild("leaderstats").Yums
local Burger = Player:WaitForChild("leaderstats").burger

About the RemoteEvent on top of your script, if you want to ensure that it won’t reduce 1000 so that the player will have -1000, then add an if statement like this :

if	player.leaderstats.Yums.Value >=  1000  then 
	player.leaderstats.Yums.Value -= 1000
end

4)You can shorten your code by using ‘-=’ [to reduce’ or ‘+=’ [to add]

2 Likes

okey thanks I think that helps especially waitforchild because it might also run too fast

1 Like

No problems! Make sure to mark it as a solution if it solved your issue!

Indeed, sometimes stuff load before other stuff, hence you’d have to use WaitForChild()

1 Like

We can use Yucon Framework made by IGottic

Anything that is not wait and works perfect

1 Like

thanks for the help I think it works

1 Like

Yes, I am talking about that they use delta for wait so it is more accurate
https://devforum.roblox.com/t/yucon-framework-next-level-optimizing-and-organizing

2 Likes

Oh well, that’s nice! Never saw that before

It optimize your code and less memory leak

1 Like
local leaderstats = player.leaderstats
local yums = leaderstats.Yums
yums.Value = if yums.Value >= 1000 then yums.Value - 1000 else yums.Value
if player.leaderstats.Yums.Value >= 1000 then 
	player.leaderstats.Yums.Value -= 1000
end

It’s preferred to Wait for the leaderstats to load up when its on the client, since scripts load before the character

I was showing you how you could shorten your already shortened snippet of code (where no WaitForChild calls are present).

if player.leaderstats.Yums.Value - 1000 < 0 then
	return
else
	player.leaderstats.Yums.Value -= 1000
end

I saw, thank you. Sometimes I work with the long ways and miss the short ones…

But usually it prints an error to the output that “leaderstats is not a valid memeber of Player”
So since that I decided to use WaitForChild, and it actually helped.

Yes it helped and I will also apply forummer’s option

Thank you.

Forummer’s option shortens the way I gave you, so they’re basically the same.
Choose what you feel the best with :slight_smile:

Also, it is not something that just makes your code not laggy. When speaking in terms of optimization, the framework seeks to encourage optimization, not just create it.

Very informative, but wait is still deprecated, are they going to make it function better than its now?

Yeah, IGottic is trying to make it more efficient to make it work better.