0
votes

So im new to data storing on roblox from tutorials this is what I have come up with I use prints to figure out everything runs fine just on the player removing function when its supposed to save the data it always returns nil and dosent save ive tried over and over can someone explain to me what im doing wrong?

ive tried changing multiple things for about two days now I just want to figure this out.

game.Players.PlayerRemoving:connect(function(plyr)

 succsess, err = pcall(function()
    if succsess then
    local ttt = plyr.PlayerGui:findFirstChild("SavedSpot")
        savespot:SetAsync(plyr.UserId.."-SaveNumber",ttt.Value)
        print("Saved")
    else
      warn(err)
        print("No Save")
    end
    playersleft = playersleft - 1
    be:Fire()
end)

end)

the player enters the game the data tries to load if the player has never played the data returns nil and makes the value 1 I have separate code in a block when you touch it to set the value to two when the person leaves e.g. the player removing function I want it to save the value two and then load it when the player enters again my only error message I receive is nil

2

2 Answers

0
votes
heres the full code incase you need it 



local ds = game:GetService("DataStoreService")
local savespot = ds:GetDataStore("Spot")
local timesplyd = ds:GetDataStore("TimesPlayed")
local playersleft = 0
print("starting")




game.Players.PlayerAdded:connect(function(plyr)
playersleft = playersleft + 1
print("Loading")
plyr.CharacterAdded:connect(function(char)
repeat wait() until plyr.PlayerGui:findFirstChild("SavedSpot")
repeat wait() until char:findFirstChild("Humanoid")
local sss =     plyr.PlayerGui:findFirstChild("SavedSpot")
local ss
print("Loaded")
local tp
su,er = pcall(function()
ss = savespot:GetAsync(plyr.UserId.."-SaveNumber")
if su then
print("Loaded")
elseif er then
print(er)
end
end)
if ss ~= nil then
sss.Value = ss
print(ss)
else
sss.Value = 1
print("1111111111111111111")
end

end)
end)
local be = Instance.new("BindableEvent")

game.Players.PlayerRemoving:connect(function(plyr)


succsess, err = pcall(function()
if succsess then
local ttt = plyr.PlayerGui:findFirstChild("SavedSpot")
savespot:SetAsync(plyr.UserId.."-SaveNumber",ttt.Value)
print("Saved")
else
warn(err)
print("No Save")
end
playersleft = playersleft - 1
be:Fire()
end)
end)

game:BindToClose(function()
be.Event:Wait()
end)
0
votes

Make sure you're running the code in a Script and not a LocalScript. Also, you probably shouldn't try to check if there was no errors from inside the pcall statement. Also, SetAsync is supposed to return nil.

success, err = pcall(function()
    local ttt = plyr.PlayerGui:findFirstChild("SavedSpot")
    savespot:SetAsync(plyr.UserId .. "-SaveNumber", ttt.Value)
end)

if(success) then
    print("Saved successfully!")
else
    print("Save error!")
    warn(err)
end