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

Value goes back up when you purchase something?

Hello! I have been creating a shop for the past few hours, but there's a problem: the money goes back up in a second after you purchase something. So, let's say there is something in the shop for 1 thousand cash, and you buy it and expect for your money to go down. Except the opposite happens. You have 5 thousand cash and you purchase the gear for 1 thousand. The money goes down to 4 thousand for a few seconds, then goes back to 5 thousand. I have a currency riser in the game, but it increases the value by 30 each 1 second. Heres the currency riser script:

while true do wait(1)
	player.leaderstats[valueName].Value = player.leaderstats[valueName].Value + 30
end

And heres the shop script:

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Parent.Parent.Parent.Clicked:Play()
	if game:GetService("Players").LocalPlayer.leaderstats["Money"].Value <= 1001 then
		game:GetService("Players").LocalPlayer.leaderstats["Money"].Value = game:GetService("Players").LocalPlayer.leaderstats["Money"].Value - 1000
		local balloon = game.Lighting.RedBalloon:Clone()
		balloon.Parent = game.Players.LocalPlayer.Backpack
	end
end)

All help will be appreciated! :)

1 answer

If you change the values from a localscript it will only change the value from the client, you should use remote events to change the values from the server.

LocalScript

script.Parent.MouseButton1Click:Connect(function()
local replicatedstorage = game:GetService("ReplicatedStorage")
local PurchaseRemote = replicatedstorage.PurchaseRemote --Add a remoteevent into replicatedstorage named PurchaseRemote
    script.Parent.Parent.Parent.Parent.Parent.Clicked:Play()
    if game:GetService("Players").LocalPlayer.leaderstats["Money"].Value <= 1001 then
       PurchaseRemote:FireServer()
    end
end)

Normal Script

local replicatedstorage = game:GetService("ReplicatedStorage")
local PurchaseRemote = replicatedstorage.PurchaseRemote

PurchaseRemote.OnServerEvent:Connect(function(player)
	player.leaderstats.Money.Value = player.leaderstats.Money.Value - 1000
	local balloon = game.Lighting.RedBalloon:Clone()
        balloon.Parent = game.Players.LocalPlayer.Backpack
end)

5 comments

0

Im using a server script...

0

It’s definitely in a local script cause you wouldn’t be able to get the LocalPlayer the way you did in a script.

0

You cant use localplayer in a server script nor gui functions im pretty sure

0

that still doesnt make it NOT GO BACK UP though lol

0

It is why. To reference the player in a LocalScript, you would use LocalPlayer. In a server script, there are multiple ways. However, for your shop script since it's on the Client, it is not able to change your leaderstats. That's something the server is supposed to do because leaderstats are for everyone to see.

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