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

i'm getting this error "attempt to index a nil value" how do i stop it?

i'm making a claim for a tycoon game and i'm getting this error for the claim. the script is still doing what i want it to just it's still giving the error

"Workspace.Claim.GateControl:8: attempt to index a nil value"

this is the script that i have

local BValue = script.Parent.BValue
local Owner = script.Parent.Owner
local BillBoard = script.Parent.BillBoard.SurfaceGui.TextLabel

script.Parent.Head.Touched:connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local playerName = hit.Parent.Name
	local Owning = game.Players:FindFirstChild(playerName).Owning
		if player ~= nil then
			if hit.Parent:FindFirstChild("Humanoid") then
				if hit.Parent.Humanoid.Health > 0 then
					if BValue.Value == false then
						if Owning.Value == false then
							Owner.Value = playerName
								BillBoard.Text = playerName
									BValue.Value = true
										Owning.Value = true
										script.Parent.Colour.BrickColor = 				 
 BrickColor.Green()
					end			
				end
			end
		end
	end
end)

3 comments

0

Mother of god.

0

So much nesting.  You can also use "and' and "elseif" op.

0

and ":Connect"

1 answer

✓ Accepted answer

While your issue is specifically with line 8, there's a lot of unneeded code in your script that could be cleaned up. Your issue is that you're checking if the player exists after you're checking for player.Owning, which means that if anything besides a player touches the head then it will error. This specific issue can be fixed by swapping lines 8 and 9, but a bunch of the code you've written is unneeded.

Another issue with your code (your script will still work if you ignore this) is that you're defining a variable and then basically redefining it two lines later. You're creating a variable for player, but then you're checking game.Players for something of the player's name (which will return the player if the player exists). This is like writing x.Parent:FindFirstChild(x.Name), it's pointless and you already have the variable you need (in the example, x). Since you already have a variable for the player, you can shorten line 8 (line 9 with fix) to local Owning = player.Owning. One more thing with your code that doesn't matter as much: if player ~= nil is the same thing as writing if player, no need to compare to nil.

1 comment

0

thanks i mooved line 8 to line 12

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