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

Roblox script only saves data once in a random chance, why?

Asked by 4 years ago
Edited 4 years ago

So I finished a script or as some might say copied it and just changed some names to what I want and here it is.

`` local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "Fame"
cash.Parent = leaderstats

local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-cash")
end)

if success then
    cash.Value = data
else
    print("There was an error while getting your data")
    warn("errormessage")
end

end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
myDataStore:SetAsync(player.userId.."-cash",player.leaderstats.Fame.Value)
end)

if success then
    print("Player Data successfully saved!")
else
    print("There was error when saving data")
    warn(errormessage)
    end

end)

````

Now when I press run there are no errors and I give myself cash using "game.Players.MrMeeper9.leaderstats.Fame.Value = 900" and it doesn't save. I try it again and no saving. I change the name back to cash and it saves, yes! but that's not it. I try again but switching the value to 0 and it doesn't save. I try again and again and it doesn't work. Now I change the name back to fame and then, guess what?, it doesn't work. So I try a bit and more and change back to cash as the name and this time I try it super fast before the rest of the stuff can load in output, and it worked! Now I try going slow and fast and waiting and not but it just doesn't save when it needs to. Any ideas?

Answer this question