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)

7 comments
http://robloxdev.com/articles/Writing-Clean-Code
???
i think he's saying it's hard to understand a code when it is not neat
btw idk if i'm spoon feeding this bu to fix this problem is simple: your script makes it check if it is not an accessory, but you should make it check if it has a humanoid instead
ill try that, but why isnt this working?
i am not entirely sure, what line of the script had the error?
I forget how an accessory is structured, but if it's Accessory->Handle, then the script is detecting the handle (which is not an Accessory, it's a Part), and thinking "Yeah this is a player, it's fine", but it's not fine. Just go with Zendaya and check for humanoid instead as that's the way most people check if the toucher is actually a player.