Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why am I getting 'attempt to index nil'?

Asked by
XRed03 17
2 years ago
Edited 2 years ago

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

Log in to vote
0
Answered by 2 years ago

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. `

Ad
Log in to vote
0
Answered by 2 years ago

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.

Answer this question