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

If statement only checks condition once?

Asked by 4 years ago

So I have this code here

script.Parent.Touched:Connect(function(plr)
    local humanoidcheck = plr.Parent:FindFirstChild("Humanoid")
    if (humanoidcheck) then
        local player = game.Players:GetPlayerFromCharacter(humanoidcheck.Parent);
        if (player) then
        local orbfolder = player.orbfolder
        if orbfolder.orb1.Value == 0 then
        local orbs = player.leaderstats.Orbs
        orbs.Value = orbs.Value + 1
        orbfolder.orb1.Value = 1
    end
    end
    end
    end)

Its all fine and dandy till the

if orbfolder.orb1.Value == 0 then

section where this if statement seems to only check once. I've tested this in studio setting it to 1 and it still gives it, and then after this i set it to zero and it doesnt award it. Im not sure whats going on here, any help is appreciated!

0
From what I can see, it checks once, and then sets orbfolder.orb1.Value to 1, which is why the if statement is no longer true sheepposu 561 — 4y
0
Yes but the problem is if I go into testing mode and put 1 as the orb1 value in the player it still gives the darn thing. And if after it gives the orb, I set the value back to 0 it wont give again spot6003 64 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

If statements are supposed to only check something once. If you are trying to make it constantly check while player is touching, do something like this:

local touching

script.Parent.Touched:Connect(function(plr)
    touching = true
    local humanoidcheck = plr.Parent:FindFirstChild("Humanoid")
    if (humanoidcheck) then
        local player = game.Players:GetPlayerFromCharacter(humanoidcheck.Parent);
            if (player) then
            local orbfolder = player.orbfolder
                while touching == false do
                    if orbfolder.orb1.Value == 0 then
                        local orbs = player.leaderstats.Orbs
                        orbs.Value = orbs.Value + 1
                        orbfolder.orb1.Value = 1
                    end

                end
            end
    end
end)
part.TouchEnded:Connect(function(plr)
    touching = false

I would also recommend lining everything up to make it neater and easier to read. If you have any errors just comment on here!

Ad

Answer this question