I have a script that requires you to touch a brick to activate it. It works, but it spams console "Torso is not a valid member of Accessory" so to try and fix that, I added a check to make sure the part touching wasn't an accessory, and its not working. Any help? PLEASE DO NOT SPOONFEED ME.
local debounce = false script.Parent.Touched:Connect(function(plr) if plr.ClassName ~= "Accessory" then local pos1 = plr.Parent.Torso.Position local pos2 = script.Parent.Base.Position local magn = (pos1-pos2).magnitude if magn < 5 then local humanoid = plr.Parent.Humanoid if not debounce then debounce = true if script.Parent.tier.Value == 1 then local deter = math.random(1,3) if deter == 1 then humanoid.WalkSpeed = humanoid.WalkSpeed + 5 elseif deter == 2 then humanoid.JumpPower = humanoid.JumpPower + 20 elseif deter == 3 then if humanoid.Health + 30 > humanoid.MaxHealth then local difference = humanoid.Health + 30 - humanoid.MaxHealth humanoid.MaxHealth = humanoid.MaxHealth + difference humanoid.Health = humanoid.Health + 30 else humanoid.Health = humanoid.Health + 30 end end elseif script.Parent.tier.Value == 2 then local deter = math.random(1,3) local deter = 3 if deter == 1 then humanoid.WalkSpeed = humanoid.WalkSpeed + 30 elseif deter == 2 then humanoid.JumpPower = humanoid.JumpPower + 50 elseif deter == 3 then if humanoid.Health + 50 > humanoid.MaxHealth then local difference = humanoid.Health + 50 - humanoid.MaxHealth humanoid.MaxHealth = humanoid.MaxHealth + difference humanoid.Health = humanoid.Health + 50 else humanoid.Health = humanoid.Health + 50 end end elseif script.Parent.tier.Value == 3 then -- local deter = math.random(1,3) local deter = 2 if deter == 1 then humanoid.WalkSpeed = humanoid.WalkSpeed + 20 --wow that was eazy elseif deter == 2 then game.ReplicatedStorage.RemoteEvent:FireClient(plr,".4","basic") deter = 1 elseif deter == 3 then local difference = humanoid.Health + 100 - humanoid.MaxHealth humanoid.MaxHealth = humanoid.MaxHealth + difference humanoid.Health = humanoid.Health + 100 else humanoid.Health = humanoid.Health + 100 end elseif script.Parent.tier.Value == 4 then local deter = math.random(1,3) if deter == 1 then humanoid.WalkSpeed = humanoid.WalkSpeed + 35 elseif deter == 2 then --having issues with spawning swords, sry deter = 1 elseif deter == 3 then local difference = humanoid.Health + 175 - humanoid.MaxHealth humanoid.MaxHealth = humanoid.MaxHealth + difference humanoid.Health = humanoid.Health + 175 else humanoid.Health = humanoid.Health + 175 end elseif script.Parent.tier.Value == 5 then local deter = math.random(1,3) if deter == 1 then humanoid.WalkSpeed = humanoid.WalkSpeed + 50 elseif deter == 1 then --having issues with spawning swords, sry deter = 1 elseif deter == 3 then local difference = humanoid.Health + 300 - humanoid.MaxHealth humanoid.MaxHealth = humanoid.MaxHealth + difference humanoid.Health = humanoid.Health + 300 else humanoid.Health = humanoid.Health + 300 end end wait(14.2)--might change to call from a value outside of the script to avoid bugs, but idk debounce = false end end end end)
The problem is that you aren't checking to see if the part that was touched is a direct child of the player's character.
You should check to see if a Humanoid
child exists within the touched part's parent at the start of the function, otherwise don't do anything.
Hope this helps :)