Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to fix leaderstats not resetting?

im trying to make a gui where you can sell something here is the code for that

local player = game.Players.LocalPlayer
script.Parent.Parent.TextButton.MouseButton1Click:Connect(function()
	local leaderstats = player:WaitForChild("leaderstats")
	local Currency = player.leaderstats.Cash
	local Melon = player.leaderstats.Watermelons
	if Melon.Value > 0 then
		Currency.Value = Currency.Value + Melon.Value *5
		Melon.Value = 0
	end
end)

there is also a part that will add to the thing that you can sell Watermelons in this case

local buttonPressed = false

script.Parent.Touched:Connect(function(hit)
	if not buttonPressed then
		buttonPressed = true
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		script.Parent.Transparency = 1
		script.Parent.CanCollide = false
		player.leaderstats.Watermelons.Value = player.leaderstats.Watermelons.Value + 1
		wait(1)
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
		buttonPressed = false
	end
end)

but after you sell them if you touch the brick again it adds the to value you had before selling how would i fix this to make the melon count start over a zero

1 answer

✓ Accepted answer

In the first script, you change the value in what I presume is a local script. This will only change the value on your client. The server won't detect this change. I would create a Remote Event, and then create a server script that responds to those changes.

(The remote event is located in replicated storage, so it is replicated on the client as well)

In your local script:

local player = game.Players.LocalPlayer
local convertWater = game.ReplicatedStorage:WaitForChild("ConvertWatermelons")

script.Parent.Parent.TextButton.MouseButton1Click:Connect(function()
	convertWater:FireServer()
end)

and in a server script:

local convertWater = game.ReplicatedStorage:WaitForChild("ConvertWatermelons")

convertWater.OnServerEvent:Connect(function(plyr)
    local leaderstats = plyr:WaitForChild("leaderstats")
    local Currency = plyr.leaderstats.Cash
    local Melon = plyr.leaderstats.Watermelons
    if Melon.Value > 0 then
        Currency.Value = Currency.Value + Melon.Value *5
        Melon.Value = 0
    end
end)

1 comment

0

And here's an article describing remote events: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

This discussion is preserved as it was. New answers, comments, and votes are closed.