Datastore Help One Line Not Working

hey guys, there’s this weird thing where a line of code doesn’t print, nor does it error. I used this code in other games and it works

game.Players.PlayerRemoving:Connect(function(plr)
		local success, errormessage = pcall(function()
			NewDS:SetAsync(plr.UserId.."-money",plr.leaderstats.Money.Value) -- this line
			print("noooss")
		end)
		if success then
			print("yay")
		else
			print("noooo")
			warn(errormessage)
		end
		
		local Player = plr.PlayerGui.SurfaceGui.ScrollingFrame:WaitForChild(plr.Name)
		Player:Destroy()
		local Money = plr.PlayerGui.SurfaceGui.ScrollingFrame.Money
		Player:Destroy()
		value.Value = value.Value - 50
	    end)
end)

Is studio access to API services enabled? Also you might need to test outside of studio. BTW, why is there another end at the end of your code after value.Value

game.Players.PlayerRemoving:Connect(function(plr)
		local success, errormessage = pcall(function()
			NewDS:SetAsync(plr.UserId.."-money",plr.leaderstats.Money.Value) -- this line
			print("noooss")
		end)
		if success then
			print("yay")
		else
			print("noooo")
			warn(errormessage)
		end
		
		local Player = plr.PlayerGui.SurfaceGui.ScrollingFrame:WaitForChild(plr.Name)
		Player:Destroy()
		local Money = plr.PlayerGui.SurfaceGui.ScrollingFrame.Money
		Player:Destroy()
		value.Value = value.Value - 50
	    end)
end)

yes API is enabled (characters)

You should save user’s stats somewhere other than inside leaderstats folder like serverstorage so when PlayerRemoving event fires you can have a chance to save the player data.

1 Like

You have an error in your code check my previous post.

I’ve saved stats located inside the player a lot nothing happens when it’s inside the player, though I think saving in server storage also works too and is also reliable though I normally just put it inside player for easier referencing.

It’s 100% possible for the player to get GC’ed whenever they leave the game before data is saved.

GC stands for Garbage Collection which is program just freeing up memory by removing stuff that is no longer referenced and used.

1 Like
local DSS = game:GetService("DataStoreService")
local NewDS = DSS:GetDataStore("MyDataStore")
local value = workspace.Ok

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = plr

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = stats
	
	
	local data
	local success, errormessage = pcall(function()
		data = NewDS:GetAsync(plr.UserId.."-Money")
	end)
	if success then
		money.Value = data
		print("yay")
	else
		print("Oh no")
		warn(errormessage)
	end
	
	local UI = Instance.new("TextLabel")
	UI.Name = (plr.Name)
	UI.Parent = plr.PlayerGui:WaitForChild("SurfaceGui").ScrollingFrame
	UI.Text = (plr.Name)
	UI.Size = UDim2.new(0,450,0,50)
	UI.Position = UDim2.new(0,0,0,value.Value) 

	local UI2 = Instance.new("TextLabel")
	UI2.Name = "Money"
	UI2.Parent = plr.PlayerGui.SurfaceGui.ScrollingFrame
	UI2.Text = (money.Value)
	UI2.Size = UDim2.new(0,100,0,50)
	UI2.Position = UDim2.new(0,0,0,value.Value)

	value.Value = value.Value +50
end)

	game.Players.PlayerRemoving:Connect(function(plr)
		local success, errormessage = pcall(function()
			NewDS:SetAsync(plr.UserId.."-money",plr.leaderstats.Money.Value)
			print("noooss")
		end)
		if success then
			print("yay")
		else
			print("noooo")
			warn(errormessage)
		end
		
		local Player = plr.PlayerGui.SurfaceGui.ScrollingFrame:WaitForChild(plr.Name)
		Player:Destroy()
		local Money = plr.PlayerGui.SurfaceGui.ScrollingFrame.Money
		Player:Destroy()
		value.Value = value.Value - 50
end)

Full code

Ok, try defining the player’s money before doing the pcall function so we can still access it even if the player’s values are removed when they leave. You’re testing outside of studio right? No errors?

Just because it’s never happened to you doesn’t mean it isn’t possible, it’s generally a bad idea to give advice based on “it hasn’t happened to me so it’s impossible to happen to you.” Factors like the amount of data and type of data need to be taken into consideration as well.

1 Like

Oh ok, my bad. (Characterssssss) There i’ve deleted the post sorry about that.

no errors relating to the script

Ok, are you testing outside of studio?

local DSS = game:GetService("DataStoreService")
local NewDS = DSS:GetDataStore("MyDataStore")
local value = workspace.Ok

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = plr

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = stats
	
	
	local data
	local success, errormessage = pcall(function()
		data = NewDS:GetAsync(plr.UserId.."-Money")
	end)
	if success then
		money.Value = data
		print("yay")
	else
		print("Oh no")
		warn(errormessage)
	end
	
	local UI = Instance.new("TextLabel")
	UI.Name = (plr.Name)
	UI.Parent = plr.PlayerGui:WaitForChild("SurfaceGui").ScrollingFrame
	UI.Text = (plr.Name)
	UI.Size = UDim2.new(0,450,0,50)
	UI.Position = UDim2.new(0,0,0,value.Value) 

	local UI2 = Instance.new("TextLabel")
	UI2.Name = "Money"
	UI2.Parent = plr.PlayerGui.SurfaceGui.ScrollingFrame
	UI2.Text = (money.Value)
	UI2.Size = UDim2.new(0,100,0,50)
	UI2.Position = UDim2.new(0,0,0,value.Value)

	value.Value = value.Value +50
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
		NewDS:SetAsync(plr.UserId.."-Money",plr.leaderstats.Money.Value)
		print("noooss")
	end)
	if success then
		print("yay")
	else
		print("noooo")
		warn(errormessage)
	end
		
	local Player = plr.PlayerGui.SurfaceGui.ScrollingFrame:WaitForChild(plr.Name)
	Player:Destroy()
	local Money = plr.PlayerGui.SurfaceGui.ScrollingFrame.Money
	Player:Destroy()
	value.Value = value.Value - 50
end)

I just edited the script now try again. The key you were saving data to wasn’t the same key you were getting data from. Also, remove the print in your pcall it may be part of the issue.

1 Like

Use data store 2 no issues! Its really easy.