local deb = false script.Parent.Touched:connect(function(h) if h.Parent:FindFirstChild("Humanoid") then if deb == true then return end deb = true script.Parent.Sound4:Play() local player = game.Players:GetPlayerFromCharacter(h.Parent) if player.Character:FindFirstChild("Left Leg") or player.Character:FindFirstChild("Right Leg") then if player.Character:FindFirstChild("Left Leg") then player.Character["Left Leg"]:Destroy() end if player.Character:FindFirstChild("Right Leg") then player.Character["Right Leg"]:Destroy() end end elseif player.Character:FindFirstChild("Left Leg") ~= nil and player.Character:FindFirstChild("Right Leg") ~= nil then if player.Character:FindFirstChild("Torso") then player.Character.Torso:Destroy() end end deb = false end)
Touching this brick is supposed to destroy the player's legs. If they touch it again while they don't have any legs, their torso will be destroyed. The part where the legs get destroyed works fine, however for some reason I get an "attempt to index field 'Parent' (a nil value)" when I touch it again.
local ready = true --starting with a debounce var as true simplifies the debounce if statement. I called it 'ready' to increase human readability. ('if condition and ready' reads much better) script.Parent.Touched:connect(function(h) if h.Parent:FindFirstChild("Humanoid") and ready then --verify humanoid and debounce ready = false --switch debounce script.Parent.Sound4:Play() --you didn't really need this line, at least for what you included in ScriptingHelpers if h:FindFirstChild("Left Leg") or h:FindFirstChild("Right Leg") then --h is still the character, so you don't have to go through the player. if h:FindFirstChild("Left Leg") then --this will always destroy the left leg first. You can switch this if you want the right leg destroyed first. h["Left Leg"]:Destroy() else h["Right Leg"]:Destroy() end elseif h:FindFirstChild("Torso") then h["Torso"]:Destroy() --you used (paraphrased) h.Torso:Destroy(). That's likely your problem end ready = true end end)
Its not h use hit
view source
local deb = false
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then if deb == true then return end deb = true
script.Parent.Sound4:Play()
local player = game.Players:GetPlayerFromCharacter(hit.Parent)<---only use this for GUIS Touch to make a gui pop up
Use this
player = hit.Parent if player:FindFirstChild("Left Leg") or player:FindFirstChild("Right Leg") then
if player:FindFirstChild("Left Leg") then player["Left Leg"]:Destroy() end if player:FindFirstChild("Right Leg") then player[Right Leg"]:Destroy() end end elseif player:FindFirstChild("Left Leg") ~= nil and player:FindFirstChild("Right Leg") ~= nil then if player:FindFirstChild("Torso") then player.Torso:Destroy() end end deb = false end)
--hit.Parent means Character that touch the part that is in the workspace