Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

money stats not working correctly, help?

Asked by 6 years ago
Edited 6 years ago

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.

0
You shouldn’t use ChildAdded. Instead use PlayerAdded. User#19524 175 — 6y

2 answers

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

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.

0
Another thing to note: You should check if the player has enough money on the server, because the player could change the amount of money they have using an exploit, which I've already done in the code cringe_worthey 97 — 6y
0
It says " RemoteEvent is not a valid member of ReplicatedStorage"- what should I do? I think I put the script in the wrong spot maybe? I put it in the Textbutton in my screenGUI. yoshikins101 11 — 6y
0
You create the RemoteEvent and parent it to ReplicatedStorage cringe_worthey 97 — 6y
0
Ok! Is there anything I should do with said RemoteEvent? I did that, and now it says " Connect is not a valid member of RemoteEvent"-- sorry for asking so many questions! ^^'' I'm very new to this kind of thing. yoshikins101 11 — 6y
View all comments (4 more)
0
Oh, that's because I forgot to put .OnServerEvent before :Connect. I've edited the code to make it work cringe_worthey 97 — 6y
0
Ok, thank you! It seems to work now in studio mode, but not in a public server- but I'll keep messing around with it! yoshikins101 11 — 6y
0
Show the code for both scripts and say where you parented them both cringe_worthey 97 — 6y
0
Ok! I'll post it in the answers section in a few moments. yoshikins101 11 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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.

0
Here's your problem: Server scripts don't work when they're parented to StarterGui, so I suggest you parent it to ServerScriptService cringe_worthey 97 — 6y
0
Oh! It works now! Thank you so much for the help + fast response!! yoshikins101 11 — 6y
0
If it worked, please accept my answer cringe_worthey 97 — 6y
0
? What do you mean? I'm new to this website- I just registered today, in fact- so if its something I can do on the website, im a little lost xD yoshikins101 11 — 6y

Answer this question