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

Data store not loading/saving?

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
--	if game.Workspace.DistributedGameTime > 3 then
--	repeat wait(1) until game.Workspace.DistributedGameTime > 3
--	end
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		myDataStore:GetAsync(player.UserId.."-stage")
	end)
	
	if success then
		stage.Value = data
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
local StageValue = player.leaderstats.Stage.Value
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-stage", player.leaderstats.Stage.Value)
	end)
	
	if success then
		print("Data saved")
	else
		warn(errormessage)
	end
end)

Here is my script, I followed a tutorial online and got this. It doesn't work. Before you say this is a copy of my previous post, no one will answer it since there is already an answer (that doesn't work.) Now before you post an answer please check if it works because I don't want this to happen again. No errors, Data saved was printed, but I don't get the stat number back when joining, am I doing something wrong? The tutorial I watched was this. I've also looked in other places for data stores, none worked. Am I doing something wrong?

4 comments

1

On line 19, you define data, but you fail to give it a value on line 21. Shouldn't line 21 be something like data = myDataStore:GetAsync(player.UserId.."-stage")?

1

Side note: You don't have to define a new function when using pcall. Here's an example of how to avoid that in your case: local success, data = pcall(myDataStore.GetAsync, myDataStore, player.UserId.."-stage")

1

If the pcall errors, data would be the error message. If pcall does not error, then data would be the return value of the GetAsync method.

0

Thank you!

2 answers

✓ Accepted answer

You aren't setting the data.

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
--  if game.Workspace.DistributedGameTime > 3 then
--  repeat wait(1) until game.Workspace.DistributedGameTime > 3
--  end

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local stage = Instance.new("IntValue")
    stage.Name = "Stage"
    stage.Parent = leaderstats

    local success, returnval = pcall(function()
       return myDataStore:GetAsync(player.UserId.."-stage")
    end)

    if success then
        stage.Value = returnval
    else
        warn(returnval)
    end
end)

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

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId.."-stage", player.leaderstats.Stage.Value)
    end)

    if success then
        print("Data saved")
    else
        warn(errormessage)
    end
end)

2 comments

0

thank you!

0

No problem, happy to help!

You might've forgotten to enable API services for your game. If so you can just go to Configure game > Enable API services.

Otherwise i don't know what could be the problem, your script seems fine.

2 comments

0

That is on, it was a previous problem, thank you for trying

0

He would've got some kind of error/warning if that were the case. Maybe you should've read his question/code a little more carefully before posting such a trite answer.

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