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

Datastore not loading correctly?

When I click play the message 'err' loads up , what is causing this?

local DataStoreService = game:GetService("DataStoreService")
local http = game:GetService("HttpService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

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

	local Leaderstats = Instance.new("Folder",player)
	Leaderstats.Name = "Leaderstats"

	local Shards = Instance.new("IntValue",Leaderstats)
	Shards.Name = "Shards"

	local Level = Instance.new("IntValue",Leaderstats)
	Level.Name = "Level"

	local data
	local suc, err = pcall(function()
	data = http:JSONDecode(PlayerData:GetAsync(player.UserId))		
	end)
	
	if suc then

	if data then

	player.Leaderstats.Shards.Value = data.Shards
	player.Leaderstats.Level.Value = data.Level 
	player:LoadCharacterWithHumanoidDescription(data.humDescription) -- load it with the description

	else

	player.Leaderstats.Shards.Value = 0 -- ur default value
	player.Leaderstats.Level.Value = 0 -- same here

end

else

warn('err')

	end

end)

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

	local data = {

		Shards = player.Leaderstats.Shards.Value,
		Level = player.Leaderstats.Level.Value,
		humDescription = game.Players:GetHumanoidDescriptionFromUserId(player.UserId) -- save the description

	}

	PlayerData:SetAsync(http:JSONEncode(data))

end)

6 comments

0

probably the warn('err')............... duh

0

well obv , its there to tell me something went wrong right? @greatneil80

0

try print (err), the variable value

0

sayingggg Argument 1 missing or nil @greatneil80 @InedibleGames

0

Basically, the datastore didn't load correctly, so suc (short for success) was false. The code was warn('err'), replace with warn(err)

0

Saying Arg 1 missing or nil when loading in and Arg 2 missing or nil when exiting @TTChaos

1 answer

✓ Accepted answer

Well, you are using the format of SetAsync completely wrong. SetAsync has two arguments, first is the PlayerKey (or PlayerUserId) [Basically, the name of the data of the player], and the second is the data. Also, now, you don't need to have JSONEncode for the table to save in Datastore. Tables, now can be stored directly.

Here's what your code should look :

-- Line 18
data = PlayerData:GetAsync(player.UserId) -- player.UserId is the name of the data

-- Line 54
PlayerData:SetAsync(player.UserId, data) -- player.UserId is the name and your data is directly saved as a table

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