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

Datastore LeaderStats Not Working? AGAIN

Basically I've just started creating a game and I've already got a problem.. Basically when you join the game you'll see a gui. In that gui you can choose an ability, like Aqua, Or Fire or Melee. But when ever i run this script:

local datastoresev = game:GetService("DataStoreService")
local AbilityStore1 = datastoresev:GetDataStore("AbilityStore1")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Ability = Instance.new("StringValue")
	Ability.Name = "Ability"
	Ability.Parent = leaderstats
	
	
	 playeruserid = "Player_"..player.UserId
	
	AbilityStore1:GetAsync(playeruserid)
	-- Load--
	local success, errormessage = pcall(function()
		 data = AbilityStore1:GetAsync(playeruserid)
	end)
	
	if success then
		Ability.Value = data[1]
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local data = {player.leaderstats.Ability.Value}
	local success, errormessage = pcall(function()
	AbilityStore1:SetAsync(playeruserid, data)

	end)
	if success then
		print("Data Successfully Saved") 
	else
		print("There was an error when saving player's ability")
		warn(errormessage)
	end
	
end)

Again?! Jeez I'm really not good with datastores. Now it says that on line 22 I'm trying to index nil with a number? It's the same line over and over again I don't know what I did wrong.. So sorry for making you return to this issue....

4 comments

0

I fixed the second issue, check my answer again

0

Sorry, that worked but now... it says im trying to index nil with a number, when will this trauma end?

0

It means you're indexing nil with a number... So the thing you're indexing is a number.

0

o what do i do?

1 answer

data.Ability.Value is nil, which is why it gives you that error. What you actually want is data[1], which returns the first element of data.

Solution: Ability.Value = data[1]

Hope this helped

Edit: The author has fixed the original issue. The next problem is that playeruserid is nil, replace it with player.UserId and it should now work.

2 comments

0

Ye it got rid of that error, but now the datastore isn't saving

0

It's meant to print when it saves successfully

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