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

Why is my if statements bugging??

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

so i dont even think the wait decides to happen because i tried waiting 25 seconds and the print after that didnt aappear but the one before thhe wait does... also these operators do mean that the valua is **more than **the specified number, righty? sorry about the bad grammar soethingf alergic to stung mu hand and i cant typ e well lol help is VERY muchj appreciated =)!

EDIT: i think the prboelm is my if statement but im not sure i just had to fix the title because of admin note ;P

enabled = true
function onDamage(Part)
    if Part.Parent:FindFirstChild("Humanoid") ~= nil and Part.Parent.Name ~= "script.Parent.Name" then
        if enabled == true then
        enabled = false
        print("Process started")    
        StealerName = script.Parent.Parent.Name
        BeingStolen = Part.Parent.Name
        BS = workspace:FindFirstChild(BeingStolen)
        SN = workspace:FindFirstChild(StealerName)
        print("Variables successfully defined")
        if game.Players[BeingStolen].leaderstat.Level.Value > 59 and game.Players[StealerName].leaderstat.Level.Value > 59 and game.Players[BeingStolen].leaderstat.SpecAbility.Value == 1 and game.Players[StealerName].leaderstat.SpecAbility.Value == 1 then 
        SN.Torso.Anchored = true
        BS.Torso.Anchored = true
        x = Instance.new("BoolValue")
        x.Name = "Process"
        x.Parent = SN
        print("Process value inserted into the player model of SN. Waiting 25 seconds...")
        wait(25)
        print("25 seconds waiting, processing outcome.")
        if BS and SN and BS.Humanoid.Health > 0 and SN.Humanoid.Health > 0 and SN:FindFirstChild("Process") then
        print("Outcome successful, working on changing values.")
        --if SN:FindFirstChild("Process") then
        game.Players[BeingStolen].leaderstat.Level.Value = 1
        game.Players[BeingStolen].leaderstat.EXP.Value = 60
        game.Players[BeingStolen].leaderstat.SpecAbility.Value = -999 --meant to be nothing
        Part.Parent.Humanoid.Health = 0
        game.Players[StealerName].leaderstat.SpecAbility.Value = -2 
        print("Values chaged, respawning attacker and victim.")
        SN.Humanoid.Health = 0
        BS.Humanoid.Health = 0
        wait(2)
        script.Parent:Destroy()
        end
        --end
        end
        end
    end
    print("Process ended")
end

script.Parent.Touched:connect(onDamage)
0
two words: LUA BLOCK davness 376 — 9y
0
Please format your code in lua code block. Goulstem 8144 — 9y
0
Use a Code Block so we can read it viralmoose 65 — 9y
0
sorry, is my first time using this site, i edited it :) suspiciousbox90 0 — 9y
View all comments (4 more)
0
it's leaderstat for a reason, don't mind that suspiciousbox90 0 — 9y
0
Try removing the ~ in the line script.Parent.Name =~ viralmoose 65 — 9y
0
It's because of the Part.Parent.Name ~= "script.Parent.Name" part. Remove the quotes. Tkdriverx 514 — 9y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The most suspicious part of this code is this:

Part.Parent.Name ~= "script.Parent.Name"

Notice the quotes, and how the right side is highlighted in blue? That is text. Unless you literally named a part "script.Parent.Name", this will always be true.

If you want to compare the Name of Part's Parent and this script's Parent, then drop the quotes.


Cleaning Up

This script can be made significantly cleaner.

Tab your code correctly

Use local variables

It's very unhealthy to use global variables in a function. Every new variable should be local.

Redundant Checks

Checks like ~= nil or == true are redundant and make it harder to read. It's easier to read if enabled then -- it also hints that enabled is just supposed to be a boolean that's solely used for that purpose.

leaderstats

The object is supposed to be called leaderstats, not leaderstat. As far as I know, "leaderstat" won't appear in the players list, while "leaderstats" will. If you don't want it to appear in the players list, I would suggest picking a different name so that you don't confuse yourself/anyone else. If you do want it to appear in the players list, use leaderstats.

BS and SN check

You check that BS and SN exist on line 21... But you've already used them on line 13 and 14.

That means the check on 21 will always succeed, since if they didn't exist, you'd have gotten an 8 lines earlier.

If you are certain they exist, the check is unnecessary -- and then so is :FindFirstChild. I'm guessing you aren't certain (you usually can't be). Move the check earlier.

StealerName / BeingStolen

StealerName is only used to get a player out of Players. That means you should probably not store its name, but the player themself:

local Stealer = game.Players[script.Parent.Parent.Name]

Is this supposed to be script.Parent.Name? Or is the check earlier supposed to use script.Parent.Parent.Name?

Equivalently, instead of searching for a Humanoid, we can use :GetPlayerFromCharacter:

    local BeingStolen = game.Players:GetPlayerFromCharacter(Part.Parent)
    if BeingStolen and BeingStolen.Name ~= script.Parent.Name then
        -- is script.Parent.Name supposed to be script.Parent.Parent.Name?

Victim is probably a simpler name than BeingStolen.

BS and SN

Use .Character instead of searching in the workspace for a given name!

Debounce

You set enabled = false but you never re enable it again.

Summary

Here is what I have from cleaning this up. I'm sure a lot more can be done still

local enabled = true
function onDamage(Part)
    local Victim = game.Players:GetPlayerFromCharacter(Part.Parent)
    if Victim and Victim.Name ~= script.Parent.Name then
        if enabled == true then
            enabled = false
            print("Process started")    
            local Stealer = game.Players[script.Parent.Parent.Name] -- Is this supposed to be script.Parent.Name?
            local BS = Victim.Character
            local SN = Stealer.Character
            print("Variables successfully defined")
            if BS and SN then
                if Victim.leaderstat.Level.Value > 59 and Stealer.leaderstat.Level.Value > 59 and Victim.leaderstat.SpecAbility.Value == 1 and Stealer.leaderstat.SpecAbility.Value == 1 then 
                    SN.Torso.Anchored = true
                    BS.Torso.Anchored = true
                    local x = Instance.new("BoolValue")
                    x.Name = "Process"
                    x.Parent = SN
                    print("Process value inserted into the player model of SN. Waiting 25 seconds...")
                    wait(25)
                    print("25 seconds waiting, processing outcome.")
                    if BS.Humanoid.Health > 0 and SN.Humanoid.Health > 0 and SN:FindFirstChild("Process") then
                        print("Outcome successful, working on changing values.")
                        Victim.leaderstat.Level.Value = 1
                        Victim.leaderstat.EXP.Value = 60
                        Victim.leaderstat.SpecAbility.Value = -999 --meant to be nothing
                        Part.Parent.Humanoid.Health = 0
                        Stealer.leaderstat.SpecAbility.Value = -2 
                        print("Values chaged, respawning attacker and victim.")
                        SN.Humanoid.Health = 0
                        BS.Humanoid.Health = 0
                        wait(2)
                        script.Parent:Destroy()
                    end
                end
            end
        end
    end
    print("Process ended")
end

script.Parent.Touched:connect(onDamage)
Ad

Answer this question