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 5 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

-- Lets players click a button to buy an upgrade that increases MaxSpaces
local buyButton = script.Parent 
-- Variables for the upgrade
local newSpaces = 1000000
local upgradeCost = 100000000

local function giveUpgrade(player)
    print("Someone clicked the button")
    local player = game.Players.LocalPlayer
    -- Get's the player's leaderboard to get other IntValues
    local playerStats = player:FindFirstChild("leaderstats")
    -- Gets the player's money and spaces to make changes
    local playerGold = playerStats:FindFirstChild("Gold")
    local playerSpaces = playerStats:FindFirstChild("Spaces")

    -- Checks if player has enough money to afford the upgrade
    if playerGold.Value >= upgradeCost then
        print("Player can buy item")
        -- Subtract the item's cost from the player's money
        playerGold.Value = playerGold.Value - upgradeCost
        playerSpaces.Value = newSpaces
    end

end

script.Parent.Activated:Connect(giveUpgrade)
0
Why are you defining a parameter inside of a function? That literally defeats the purpose of having a parameter. DeceptiveCaster 3761 — 5y
0
Dunno..Still, i don't think that's the problem. ady1111 8 — 5y
0
Maybe because everything is local?? Is this a normal script or a localscript? Miniller 562 — 5y
0
If he's using the LocalPlayer property of the Players service, then yes, everthing is local. User#25115 0 — 5y

1 answer

Log in to vote
0
Answered by 5 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

local buyButton = script.Parent
local newSpaces = 1000000
local upgradeCost = 100000000
--//This is fine, just declaring variables

local function giveUpgrade()
    --//We do not need to check who clicked the button, since the player is passed automatically through the RemoteFunction
    game.ReplicatedStorage.tryToPurchase:Invoke(upgradeCost, newSpaces)
    --//We shouldn't make changes to leaderstats locally, so we invoke the RemoteFunction we created earlier.
end

script.Parent.Activated:Connect(giveUpgrade)

This is the Script in game.ServerScriptStorage

game.ReplicatedStorage.tryToPurchase.OnServerInvoke = function(player, cost, spaces)
    --//We link this function to when tryToPurchase is invoked.
    --//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.

    local playerStats = player:FindFirstChild("leaderstats")
    local playerGold = playerStats:FindFirstChild("Gold")
    local playerSpaces = playerStats:FindFirstChild("Spaces")
    --//Now we can actually calculate the purchase.

    if playerGold.Value >= upgradeCost then
        playerGold.Value = playerGold.Value - cost
        playerSpaces.Value = spaces
    end
end
Ad

Answer this question