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

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

Asked by 5 years ago

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)
0
No loop or funcion. It will only save once when you join the game. voidofdeathfire 148 — 5y
0
Ill rewrite the code for you Nvm its a free model XD Whats with that code voidofdeathfire 148 — 5y
0
Wdym? MaciBoss1950 16 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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)
0
Not working MaciBoss1950 16 — 5y
0
Is it in the Game (so not in Roblox Studio) not working too? RedstonecraftHD 25 — 5y
Ad

Answer this question