Why isnt this code working?

Error:

Workspace.Quota.HitVal:9: attempt to index nil with 'Name'

current code:

local player = game:GetService("Players")
local Debounce = false

while true do 
	for i, v in ipairs(player:GetChildren()) do
		local cash = v:WaitForChild("leaderstats").Sacrifices 
		script.Parent.Touched:Connect(function(touchingPart)
			-- NORMAL NPC
			if touchingPart.Parent.Name == "NPC" and Debounce == false then
				Debounce = true
				cash.Value = cash.Value + 1
				touchingPart.Parent:Destroy()
				wait(0.1)
				script.Parent.Effects.BLOOD01.Enabled = true
				script.Parent.Effects.BLOOD02.Enabled = true
				script.Parent.Effects.BLOOD03.Enabled = true
				script.Parent.Effects.BLOOD04.Enabled = true
				script.Parent.Effects.BLOOD05.Enabled = true
				script.Parent.Effects.BloodHit.Enabled = true
				script.Parent.Blood:Play()
				script.Parent.ChainSaw:Play()
				wait(1)
				script.Parent.Effects.BLOOD01.Enabled = false
				script.Parent.Effects.BLOOD02.Enabled = false
				script.Parent.Effects.BLOOD03.Enabled = false
				script.Parent.Effects.BLOOD04.Enabled = false
				script.Parent.Effects.BLOOD05.Enabled = false
				script.Parent.Effects.BloodHit.Enabled = false
				script.Parent.Blood:Play()
				script.Parent.ChainSaw:Play()
				-- STABBING NPC
			elseif touchingPart.Parent.Name == "Stabman" and Debounce == false then
					Debounce = true
					cash.Value = cash.Value + 2
					touchingPart.Parent:Destroy()
					wait(0.1)
					script.Parent.Effects.BLOOD01.Enabled = true
					script.Parent.Effects.BLOOD02.Enabled = true
					script.Parent.Effects.BLOOD03.Enabled = true
					script.Parent.Effects.BLOOD04.Enabled = true
					script.Parent.Effects.BLOOD05.Enabled = true
					script.Parent.Effects.BloodHit.Enabled = true
					script.Parent.Blood:Play()
					script.Parent.ChainSaw:Play()
					wait(1)
					script.Parent.Effects.BLOOD01.Enabled = false
					script.Parent.Effects.BLOOD02.Enabled = false
					script.Parent.Effects.BLOOD03.Enabled = false
					script.Parent.Effects.BLOOD04.Enabled = false
					script.Parent.Effects.BLOOD05.Enabled = false
					script.Parent.Effects.BloodHit.Enabled = false
					script.Parent.Blood:Play()
					script.Parent.ChainSaw:Play()
			end
		end)
	end
	wait(1)
	Debounce = false
end

The system works fine, but I keep getting the error which is causing lag. I’m not even sure why this is happening, but I’m guessing it might be because once the block is dropped and after it deletes then the script is wondering it’s name. I have a debounce though so I’m not sure why it isn’t working. Anyone have a solution?

1 Like

whats happening is “touchingPart” doesnt have a parent object. why this is the case i have no idea. Maybe you havent set the parent ever? Maybe you deleted the object? its hard to tell

I mean I delete the object in the script, but I added a debounce to try to prevent it from checking twice, and the object does have a parent

since you have a :Connect() in a while true loop that means you are creating multiple instances of it. so when you fire the event there is a chance that 20 of the same code plays at the same time

Figured out the issue, I didn’t ask if the debounce was false first, so the code checked for a parent after it was already deleted, which returned false. Just switched the debounce with checking the touched part, which fixed the issue. Thanks.

you should still fix the “:Connect()”'s in the while true loop since it will cause unnecessary lag

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.