Why is data not saving?

script:



local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")


local function onPlayerJoin(player) -- Runs when players join
	local leaderstats = Instance.new("Folder") 
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local jump = Instance.new("IntValue") 
	jump.Name = "JumpPowerr"
	jump.Parent = leaderstats

	local playerUserId = "Player_" .. player.UserId 
	local data = playerData:GetAsync(playerUserId) 
	if data then
		
		jump.Value = data
	else
		
		jump.Value = 0
	end
end

local function onPlayerExit(player) 
	local success, err = pcall(function()
		local playerUserId = "Player" .. player.UserId
		playerData:SetAsync(playerUserId, player.leaderstats.jump.Value) 
	end)

	if not success then
		warn('Could not save data!') 
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

it just says in output
“could not save data!”

local script in starterpack:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local debounce = false
local plrpoints = plr.leaderstats.JumpPowerr
hum:GetPropertyChangedSignal("Jump"):Connect(function()

		
			if debounce then return end
				
		debounce = true
		hum.JumpPower = hum.JumpPower + 10
		plrpoints.Value = plrpoints.Value + 10
	
		wait(2)
			debounce = false
		
		
			
				
	
		
end)

2 Likes

It just says “Could not save data!” because you put that here.

To look at what the issue is just change that string to the “err” thing in the pcall and it should show in the output what the issue is. The “err” will show what the issue is.

Also by the way this should be in a pcall function (the GetAsync)

image

4 Likes

Are api services enabled in Studio? If not, enable them because you can’t save data unless they are enabled.

1 Like

it is enabled

jump is not a valid member of folder

Hey! I have made a thread on how to correctly use datastore.
I would check it out as it has everything you need and it explains it in a-lot of detail.

2 Likes

So you can now debug the issue from there.

Looking at the error from that it seems like you don’t have something called Jump inside of the folder you are trying to get it from.

1 Like

I think I have the solution to your problem. When you access the jump value, it doesnt quite exist. Try :WaitForChild() instead.

1 Like

i added WaitForChild(“jump”).Value and now it doesnt print nothing and it still doesnt work

I don’t think this is that useful for the issue the user has but still ngl quite a nice tutorial for someone who is new with Roblox datastores.

2 Likes

my new script:

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")


local function onPlayerJoin(player) -- Runs when players join
	local leaderstats = Instance.new("Folder") 
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local jump = Instance.new("IntValue",leaderstats) 
	jump.Name = "JumpPowerr"
	
	local playerUserId = "Player_" .. player.UserId 
	local data = playerData:GetAsync(playerUserId) 
	if data then

		jump.Value = data
else
      jump.Value = 0
	end
end

local function onPlayerExit(player) 
	local success, err = pcall(function()
		local playerUserId = "Player" .. player.UserId
		playerData:SetAsync(playerUserId, player.leaderstats:WaitForChild("jump").Value) 
	end)

	if not success then
		warn(err) 
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

Would it be possible for you to show us the leaderstats folder when it is in the player so I can see if you are locating it incorrectly?

1 Like

how to do that?

I believe it is something wrong with this section so if you could go and show us what it looks like in studio when ur player I can help you more.

image

1 Like

ill give a video

Go into studio and press the play button. Go to players, then click on ur player and there should be a folder called leaderstats. Open that folder and show is the insides of it.

As an example:

image


Thank you for your feedback! :smiley:
However, I do think that this would help them a-lot, has there are a-lot of things that they don’t have. (BindToClose, pcalls, etc.)

1 Like

Yea so that is your issue! Your looking for the value “jump” in the leaderstats folder but the value is called “JumpPowerr” so you should be getting the value of that.

Thats why it says there is nothing called “jump” inside of the folder cuz its not called that it is called “JumpPowerr”

1 Like