I'm working on a game. In the game, your money rises over time (you get 5 points every minute or so that you're within the game.) You can also buy things in a little gui shop.
Both of these work, but in public servers, it acts weird. For example, say you have 250 points. You buy something for 100 points, and now you have 150 points. But for some reason, after a minute or so, your points suddenly rise back up to 255 because of the script that gives you money over time. Why does that happen??
I'm a beginner to scripting, and all I know is through tutorials so far.
Here are both scripts: ((Note that "pawprints" is the type of currency in the game- its a pet simulator game.))
Shop script (note that it is a local script and located within a screenGUI textbutton)
local player = game.Players.LocalPlayer local pawprints = player.Stats.Pawprints local button = script.Parent button.MouseButton1Click:connect(function() if player.Stats.Pawprints.Value >= 150 then print (":)") player.Stats.Pawprints.Value = player.Stats.Pawprints.Value -150 else print (":(") end end)
Money rising over time script (its a normal script, not local- its located in the workspace by itself)
function GetPlayer(Player) while true do wait (60) Player.Stats.Pawprints.Value = Player.Stats.Pawprints.Value + 1 end end game.Players.ChildAdded:connect(GetPlayer)
I'm guessing the fact that the shop script is local is what's causing the other script to act odd. But I can't change either of them to normal scripts or local scripts, because they break if they do. Anyone know how to fix this?
i'm really new to scripting so i barely know anything about it so far oOPS it works just fine in studio, but not in public servers.
Your game probably FilteringEnabled on. If the player has enough money to buy something, you shouldn't subtract the player's currency from their client, and instead, from the server.
Why this happens is because any arbitrary action done on the client, with FE enabled, will not replicate to the server, and will also not be seen by other players in the game. Since your money is 250, on the client it will be seen as 100, while on the server, it's still 250. So the server-sided script will add +5 points to 250.
To solve this, use a RemoteEvent to change the money from the server. Your server and client code should look like this:
--Server game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, value) if player.Stats.Pawprints.Value >= value then player.Stats.Pawprints.Value = player.Stats.Pawprints.Value-value end end)
--Client button.MouseButton1Down:Connect(function() game.ReplicatedStorage.RemoteEvent:FireServer(150) end)
Also, if you're going to give an item to the player, give them from the server.
For both scripts, I put them in StarterGUI>ScreenGUI>Textbutton. One is a server script, and one is a local script.
Server script:
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, value) if player.Stats.Pawprints.Value >= 150 then player.Stats.Pawprints.Value = player.Stats.Pawprints.Value-150 end end)
Local script:
script.parent.MouseButton1Down:Connect(function() game.ReplicatedStorage.RemoteEvent:FireServer(150) end)
I also inserted a RemoteEvent into ReplicatedStorage, but did nothing else with it.