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

Help with Data Store please?

Asked by 8 years ago

I was attempting to learn Data Storage and I was watching a video to help with it and I did exactly as the video told me except I changed the values up a bit... and when I tested the game out on my profile it wouldn't save my wins the following script is under ServerScriptService and it's in a ServerScript Please help me out!

local datastore = game:GetService("DataStoreService"):GetDataStore("Wins")

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local wins = Instance.new("IntValue")
    wins.Value = 0
    wins.Name = "Wins"
    wins.Parent = leaderstats

    local key = "user_"..player.userId

    local savedValues = datastore:GetAsync(key)

    if savedValues then
        wins.Value = savedValues[1]
    else
        local valuesToSave = (wins.Value)
        datastore:SetAsync(key, valuesToSave)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    local key = "user_".. player.userId
    local valuesToSave = (player.leaderstats.Wins.Value)
    datastore:SetAsync(key, valuesToSave)
end)

The next script is the script I put inside of the Part which gives me wins... I chose a random amount of wins so I can point out that I have a different amount of wins easily....

script.Parent.Touched:connect(function(object)
    if debounce then return end
    debounce = true
    local humanoid = object.Parent:findFirstChild("Humanoid")
    if humanoid then
        local player = game.Players:GetPlayerFromCharacter(object.Parent)
        player.leaderstats.Wins.Value = player.leaderstats.Wins.Value +9765
        wait(2)
    end
    debounce = false
end)

Thank you for reading all the way! I hope you can help me out with this issue!

0
Consider learning it first. I've not really read your question, but it sound like you've literally skiddie'd the script from a video. User#6546 35 — 8y
1
Remove the brackets around valuesToSave when defining it's value, they're not needed. Also, remove the savedValues[1], and just make it savedValues, because savedValues is not a table. TheDeadlyPanther 2460 — 8y
0
Wow it worked -_- I was making savedValues[1] because I was gonna add some more IntValues so I thought I would make this like that so if I add more IntValues would it just become a table automatically? KingLoneCat 2642 — 8y

Answer this question