Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't my datastore work, it is pretty simple?

I change the value of Coins for ex. and it doesn't save next time I test the game

  local DS = game:GetService("DataStoreService"):GetDataStore("ScriptingTestXOXOXO19191")--DO NOT CHANGE UNLESS YOU WANT EVERYONES STATS TO BECOME 0 AGAIN!
    
game.Players.PlayerAdded:Connect(function(plr)

local stats = Instance.new("Folder",plr)

stats.Name = "leaderstats"

local Coins = Instance.new("IntValue",stats)

Coins.Name = "Coins"

local Donuts = Instance.new("IntValue",stats)

Donuts.Name = "Donuts"

local Rebirths = Instance.new("IntValue",stats)

Rebirths.Name = "Rebirths"

local key = "player-"..plr.userId

local savedValues = DS:GetAsync(key)

if savedValues then

Coins.Value = savedValues[1]

Donuts.Value = savedValues[2]

Rebirths.Value = savedValues[3]

else

local valueToSave = {Coins.Value, Donuts.Value, Rebirths.Value}

DS:SetAsync(key, valueToSave)

end

end)

  

--Saving

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

local key = "player-"..plr.userId

local stat = plr.leaderstats

local valueToSave = {stat.Coins.Value, stat.Donuts.Value, stat.Rebirths.Value}

local success, err = pcall(function()

DS:SetAsync(key, valueToSave)

end)

if success then

print("Saving was successful")

else

error("Saving was not successful")

end

end)

3 comments

0

No loop or funcion. It will only save once when you join the game.

0

Ill rewrite the code for you Nvm its a free model XD Whats with that code

0

Wdym?

1 answer

Just use this Script in and put it into ServerScriptService!

(Turn Studio API Services on!)

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 = "Cash"

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 whilst 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.Cash.Value)

end)

if success then

print("Player Data successfully saved!")

else

print("There was an error when saving data")

warn(errormessage)

end

  

end)

2 comments

0

Not working

0

Is it in the Game (so not in Roblox Studio) not working too?

This discussion is preserved as it was. New answers, comments, and votes are closed.