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

Data storage saved. Script Print Error when not saved. What is wrong with the script?

If data is saved then it will print error. My script print Error. It should have saved. what did I do wrong

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local points = Instance.new("IntValue", leaderstats)	
		points.Name = "Points"
		local PlayerID = "player"..player.UserId
		
		local PlayerData
		local success, errormessage = pcall(function()
		PlayerData = PlayerDataStore:GetSync(PlayerID)
	 	
		end)
		
		if success then
			points.Value = PlayerData
		end
		
end)

game.Players.PlayerRemoving:Connect(function(player)
	local PlayerID = "player"..player.UserId
	local data = player.leaderstats.Points.Value
	local success, errormessage = pcall(function()
			PlayerDataStore:SetSync(PlayerID, data)
	end)
	
	
	if success then 
		print ("data saved")
	else
			print ("error")
	end
end)

2 comments

0

Hmmm still error...

0

Hmm.... I added print after the first function and added print after the second function and it all ran, meaning that there is nothing wrong with my script, yet still it did not print "data saved" or "error"

2 answers

Howdy!

It's :SetAsync(), not :SetSync(). Look at line 14 and 28. You can read more about this here.

If this helped you out, consider accepting this answer for those sweet, sweet reputation points. If not, comment below and I (or someone else) will help you out.

Be sure to check out the Roblox API Documentation as well for additional reference.

5 comments

0

lol thanks

0

error still does not work

0

it doesn't print anything now

0

Weird, are you getting any error in the Output?

0

no

This is what it says

--[[
	Calls a function and throws an error if it attempts to yield.

	Pass any number of arguments to the function after the callback.

	This function supports multiple return; all results returned from the
	given function will be returned.
]]

local function resultHandler(co, ok, ...)
	if not ok then
		local message = (...)
		error(debug.traceback(co, message), 2)
	end

	if coroutine.status(co) ~= "dead" then
		error(debug.traceback(co, "Attempted to yield inside changed event!"), 2)
	end

	return ...
end

local function NoYield(callback, ...)
	local co = coroutine.create(callback)

	return resultHandler(co, coroutine.resume(co, ...))
end

return NoYield

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