Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

If statement only checks condition once?

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!

2 comments

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

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

1 answer

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.