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

Saving String Value Not Working?

Asked by 4 years ago
game.Players.ChildRemoved:Connect(function(plr)
    dsservice = game:GetService("DataStoreService")
    datastore = dsservice:GetDataStore("Rank")
    datastore:SetAsync(plr.UserId, plr.Rank.Value)
end)

game.Players.ChildAdded:Connect(function()
    dsservice = game:GetService("DataStoreService")
    datastore = dsservice:GetDataStore("Rank")
    local text = datastore:GetAsync("Rank")
    script.Parent.Rank.Value = text
end)

I Am Trying To Save A Rank But After some Tests It Doesn't Save The Rank And I Don't Know Why

2 answers

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

Try this

local datastore = game:GetService("DataStoreService")
local rankdatastore = datastore:GetDataStore("Rank")

game.Players.ChildAdded:Connect(function(plr)

    local data
    local success, errormessage = pcall(function()
        data = rankdatastore:GetAsync(plr.UserId.."-rank")
    end)

    if success then
        -- here change the rank value, for example Rank.Value = data
     else
        print("There was an error whilst getting your data")
        warn(errormessage)
    end

end)

game.Players.ChildRemoved:Connect(function(plr)
    local success, errormessage = pcall(function()
        rankdatastore:SetAsync(plr.UserId.."-rank", plr.Rank.Value)
    end)
    if success then
        print("Rank successfully saved")
    else
        print("There was an error saving data")
        warn(errormessage)
    end
end)
Ad
Log in to vote
0
Answered by
MemezyDev 172
4 years ago

it isnt working because you’re not specifying what youre getting, you are just getting ‘Rank’ but you save a value

GetAsync cant have 2 args, at least that works for me. the only arg there is the code, in which case is the players userid, what i do is attach a string to the players userid by doing datastore:GetAsync(player.UserId.. ‘-rank’) which will be able to be seperated from different players, and also be able to grab the right value. also you should wrap the :GetAsync() in a pcall function so it doesnt error incase the :GetAsync() fails. if this helped please accept my answer, else i will be happy to help you in the community chat

Answer this question