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

All of Character's Children are passing through this if statement?

Hi everyone!

I was making a game where it would detect which items you would use when you were beating an obby (For example "Boost Items Used"). Here is the code I wrote in a local script.

function check(c)
	for i, v in pairs(c:GetChildren()) do
		if v.Name=='Helicopter Crystal' or 'Low Gravity Crystal' or 'Big Boing' then
			print(v.Name) -- prints out all of the children inside the player's characdter, one by one
			game.ReplicatedStorage.ItemUsed:Fire(v.Name)
		end
	end
end

function bind(c)
	if not c then return end
	check(c)
	c.ChildAdded:Connect(function()check(c)end)
end

bind(game.Players.LocalPlayer.Character)
game.Players.LocalPlayer.CharacterAdded:Connect(bind)

For some reason all of the player character's children is detected as an item, even though their names arent 'Helicopter Crystal' or 'Low Gravity Crystal' or 'Big Boing'. Can someone help me figure out why?

Thanks in advance.

1 answer

That if was checking if the name of the part was Helicopter Crystal, if false, it would check if Low Gravity Crystal is or returns true, wich because of being a string, it does, so you need to check if the name is equals to the string in all statements:

function check(c)
    for i, v in pairs(c:GetChildren()) do
        if v.Name=='Helicopter Crystal' or v.Name=='Low Gravity Crystal' or v.Name=='Big Boing' then
            print(v.Name) -- prints out all of the children inside the player's characdter, one by one
            game.ReplicatedStorage.ItemUsed:Fire(v.Name)
        end
    end
end

function bind(c)
    if not c then return end
    check(c)
    c.ChildAdded:Connect(function()check(c)end)
end

bind(game.Players.LocalPlayer.Character)
game.Players.LocalPlayer.CharacterAdded:Connect(bind)

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