Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is if part.Classname or if part:IsA() not working???

Asked by 6 years ago

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)
0
??? OldSoul04 13 — 6y
0
i think he's saying it's hard to understand a code when it is not neat Zendaya774isstupid 50 — 6y
0
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 Zendaya774isstupid 50 — 6y
View all comments (3 more)
0
ill try that, but why isnt this working? OldSoul04 13 — 6y
0
i am not entirely sure, what line of the script had the error? Zendaya774isstupid 50 — 6y
0
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. ScrewDeath 153 — 6y

1 answer

Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago

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 :)

Ad

Answer this question