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

leadrstats Stats not updating. What should i do?

Asked by 6 years ago

So, i made a gui that gives you a better 'backpack' with X spaces for an amount of money...the problem is , the spaces becomes 100000 , the money is taken away but.....it doesn't work, i mean it's still the default option, and if i sell the items, i get the money back...

Here's the code

01-- Lets players click a button to buy an upgrade that increases MaxSpaces
02local buyButton = script.Parent
03-- Variables for the upgrade
04local newSpaces = 1000000
05local upgradeCost = 100000000
06 
07local function giveUpgrade(player)
08    print("Someone clicked the button")
09    local player = game.Players.LocalPlayer
10    -- Get's the player's leaderboard to get other IntValues
11    local playerStats = player:FindFirstChild("leaderstats")
12    -- Gets the player's money and spaces to make changes
13    local playerGold = playerStats:FindFirstChild("Gold")
14    local playerSpaces = playerStats:FindFirstChild("Spaces")
15 
View all 26 lines...
0
Why are you defining a parameter inside of a function? That literally defeats the purpose of having a parameter. DeceptiveCaster 3761 — 6y
0
Dunno..Still, i don't think that's the problem. ady1111 8 — 6y
0
Maybe because everything is local?? Is this a normal script or a localscript? Miniller 562 — 6y
0
If he's using the LocalPlayer property of the Players service, then yes, everthing is local. User#25115 0 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

If your button is on the player's screen, it's entirely clientside. This means that ONLY that player can see it, and ONLY that player can click it. This also means that any changes to leaderstats from that script will ONLY change for the player, and the server will not update. This is like editing a google doc while offline. While you can see the changes you've made, your friend cannot, and if you refresh the page you get the page that your friend is looking at. Let's see how you would fix this:

  1. First thing's first, we need to make leaderstat changes occur in a serverside script, so let's place a Script in game.ServerScriptStorage.

  2. In order for the LocalScript to communicate to the Server, we need to add a RemoteFunction to game.ReplicatedStorage. Let's name it "tryToPurchase".

This is the LocalScript inside the player's GUI

01local buyButton = script.Parent
02local newSpaces = 1000000
03local upgradeCost = 100000000
04--//This is fine, just declaring variables
05 
06local function giveUpgrade()
07    --//We do not need to check who clicked the button, since the player is passed automatically through the RemoteFunction
08    game.ReplicatedStorage.tryToPurchase:Invoke(upgradeCost, newSpaces)
09    --//We shouldn't make changes to leaderstats locally, so we invoke the RemoteFunction we created earlier.
10end
11 
12script.Parent.Activated:Connect(giveUpgrade)

This is the Script in game.ServerScriptStorage

01game.ReplicatedStorage.tryToPurchase.OnServerInvoke = function(player, cost, spaces)
02    --//We link this function to when tryToPurchase is invoked.
03    --//We create parameters for the upgradeCost and newSpaces passed along. The player who invoked the function is a hidden parameter, and we need to declare it.
04 
05    local playerStats = player:FindFirstChild("leaderstats")
06    local playerGold = playerStats:FindFirstChild("Gold")
07    local playerSpaces = playerStats:FindFirstChild("Spaces")
08    --//Now we can actually calculate the purchase.
09 
10    if playerGold.Value >= upgradeCost then
11        playerGold.Value = playerGold.Value - cost
12        playerSpaces.Value = spaces
13    end
14end
Ad

Answer this question