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

Leaderboards script not working correctly?

Asked by 4 years ago
Edited 4 years ago

Alright so I'll just oversimplifify this. I made a Leaderboard script and I placed it in ServerScriptService and then I made a Shop GUI but when I'm trying to purchase an item with the currency from my leaderboard, the "money" I have keep going down to -15 or -40 or -65 even if I have like say 99999999999 cash to buy the item, from my shop gui, it keeps going down to a negative number like -15 when purchasing that item.

the error I keep getting is : ServerScriptService.Leaderboard:24: attempt to call field 'UserId' (a number value)

So here's my Leaderboard script:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Currency")

game.Players.PlayerAdded:Connect(function(player)
    local leaderboard = Instance.new("Folder",player)
    leaderboard.Name = "Leaderboard"

    local cash = Instance.new("IntValue",leaderboard)
    cash.Name = "Cash"
    cash.Value = 50

    local recieved,failed = pcall(function()
        data = dataStore:GetAsync("User-"..player.UserId)
    end)

    if recieved then
        cash.Value = data[1]
        print("Data was received")
    else
        print("Data was not received")  
    end

    while true do wait(30)
        dataStore:SetAsync("User-"..player.UserId{cash.Value})
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local leaderboard = player.Leaderboard
    local cash = leaderboard.Cash

    local saved,failed = pcall(function()
        dataStore:SetAsync("User-"..player.UserId{cash.Value})
    end)

    if saved then
        print("Data was saved")
    else
        print("Data was not saved")
    end
end)
0
Can you give us the script for the shop thing? It may be too much removing from the stat RobloxGameingStudios 145 — 4y

Answer this question