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

Why am I getting 'attempt to index nil'?

I am trying to make a saving collectible system by using a Boolean Value called 'regularFound'. I put the value in StarterCharacterScripts, and I can collect the item just fine. However, when I try to save the value with a script, I get the error message attempt to index nil with 'regularFound'. Here is my code:

local store = game:GetService("DataStoreService")
local datastore = store:GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(plr)
	plr.Character.regularFound.Value = datastore:GetAsync(plr.UserId.. "value")
end)

game.Players.PlayerRemoving:Connect(function(plr)
	datastore:SetAsync(plr.UserId.. "value", plr.Character.regularFound)
end)

Any idea why studio thinks that 'regularFound' does not exist?

2 answers

That error usually happens when an instance doesn't exist or when an instance hasn't loaded yet. What you can maybe do is:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("regularFound").Value = datastore:GetAsync(plr.UserId.."value")
	end)
end)

This will wait for the character to load and the regularFound value to load. `

The problem is that boolean values don't support strings, they just support bools. Change the bool value to a string value.

You're welcome.

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