(SOLVED) Why is my damage script giving this error? (attempt to index local 'plr' (a nil value))

What I’m Attempting

Okay so, I’m attempting to make it so the damage only occurs when the humanoid is anything but normal… it damages the human.

The Issue

:11: attempt to index local ‘plr’ (a nil value)
This is the error I get everytime I try to use this.

Solutions I Tried

I attempted making the plr global but then the error turns into the same thing but a global one.

Extra Question

What do I need to know to work with the Humanoid?

The Script

--[[Made By MillerrIAm]]--
--[Server Variables]--
name = "Table Damage Value" --[[Change this to the name of the value you want it to read.]]--
dmg = game.ServerStorage.DamageValues[name].Value
--[Main Script]--
script.Parent.Touched:Connect(function(part)
	local plr = part.Parent:FindFirstChild("Humanoid")
	local stop = false
	if not stop then
		stop = true
		if plr.PlatformStand == true then
			plr:TakeDamage(dmg)
		end
		wait(3)
		stop = false
	end
end)

Thank you to anyone who takes the time to help me fix this.

FindFirstChild returns nil if it cant find it’s passed argument, which in this case is the humanoid. You need to check if plr is not nill before doing your other code.

1 Like

You’re assuming the humanoid exists when there’s a chance it won’t. Use better variable names though, your variable would point to a humanoid not a player

if humanoid and humanoid.PlatformStand then

You don’t need to compare booleans to another boolean, i.e true == true since you’re comparing two booleans, and == returns true or false if both operands are equal, so you’re doing all of this just to get a boolean out another two.

Also get into the habit of using local variables at the top of your script. There is -1 reason to use global variables

3 Likes

There is another slight problem with the code you show.

The ‘debounce’ will never work, as you have it specified as a local variable (stop) inside the method, where it is initialized to false every time the method is called.

To make the ‘debounce’ work better, you need to have the variable “outside” of the method. - Example:

local stop = false  -- Variable for the 'debounce' state
function onTouched(part)
  if not stop then
    stop = true
    -- ... other code here ...
    stop = false
  end
end
1 Like

So, let me get this right… you’re stating I should use a function to call upon this Debounce? I ask as my debounce isn’t working just as you stated.

I am stating, that the code you have in your first post, its usage of the stop variable seems wrong, if it is meant to be used as a ‘debounce’ feature, to ensure the player(s) is only taking damage every 3rd second.

This code of yours, its if-statement will always be true, no matter how many times the method is called, because the stop variable is always set to false, just before the if-statement, making the entire purpose of it irrelevant:

script.Parent.Touched:Connect(function(part)
	-- ...
	local stop = false
	if not stop then
		stop = true
		-- ...
		stop = false
	end
end)

But if you move the stop variable to outside of the method, then it will act as a ‘debounce’ feature, where the if-statement will sometimes be true and sometimes false, depending on the value of the stop variable:

local stop = false -- <<<<<
script.Parent.Touched:Connect(function(part)
	-- ...
	if not stop then
		stop = true
		-- ...
		stop = false
	end
end)
1 Like