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

Datastore not saving or loading?

My datastore isnt working. It's not giving me any errors or prints in the output. What is going on?

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("NewDataStore1")

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
	Cash.Value = 0

	local speed = Instance.new("IntValue")
	speed.Name = "TopSpeed"
	speed.Parent = leaderstats
	speed.Value = 16


	local playerUserId = "Player_"..player.UserId

	local data 
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)

	if success then
		--Set our data equal to the current Cash
		if data then 
			Cash.Value = data.Cash
			speed.Value = data.speed
		end
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local data = {
		Cash = player.leaderstats.Cash.Value,
		speed = player.leaderstats.TopSpeed.Value;
	}
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)

	if success then
		print("Data saved successfully")
	else 
		print("There was an error.")
		warn(errormessage)
	end


end)

1 answer

✓ Accepted answer

I found an error in your script.

40	    local data = {
41	        Cash = player.leaderstats.Cash.Value,
42	        speed = player.leaderstats.TopSpeed.Value;
43	    }

That part should be

40	    local data = {
41	        Cash = player.leaderstats.Cash.Value;
42	        speed = player.leaderstats.TopSpeed.Value
43	    }

And also this part too

27	    if success then
28	        --Set our data equal to the current Cash
29	        if data then
30	            Cash.Value = data.Cash
31	            speed.Value = data.speed
32	        end
33	    end

This should be

27	    if success then
30	    	Cash.Value = data.Cash
31	        speed.Value = data.speed
32	  end

I hope it works!

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