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

Value goes back up when you purchase something?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)
0
Im using a server script... Dan_PanMan 227 — 4y
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. SethHeinzman 284 — 4y
0
You cant use localplayer in a server script nor gui functions im pretty sure laurenbtd5 379 — 4y
0
that still doesnt make it NOT GO BACK UP though lol Dan_PanMan 227 — 4y
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. AntiWorldliness 868 — 4y
Ad

Answer this question