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

if statement runs even if its false?

so i am trying to remove the shirt of an player (if he wears one) and the remove it but if i ask if the player has a shirt and he does not the if statement still runs

if char.Shirt then
	char.Shirt:remove()
end

2 answers

✓ Accepted answer

If you are asking the player if they want to remove their shirt, but they do not have it in the first place, you need to use :FindFirstChild, which returns nil if the shirt does not exist, and references the shirt itself it exists.

Your original attempt does not work if the player does not have shirt because it is attempting to find something that does not exist in the if statement itself (not a valid member of).

local shirt = char:FindFirstChild("Shirt")
--if shirt exists, destroy it.
if shirt then
	--Use Destroy since :remove is deprecated.
	shirt:Destroy()
end

3 comments

0

thanks works now and i understand i have to try to put it with FindFirstChild in a variable and if does not exist it will turn fals Thanks

0

why did u downvote mine? Explain.

0

I didn't.

do not use :remove() use :Destroy() cough it's desperated

game.Players.PlayerAdded:Connect(function(player)--the player is added
	local char = player.Character --the character
if char.Shirt then
    char.Shirt:Destroy()
end
end)

4 comments

0

yes but its still the same problem on the player is no shirt but the if statement still runs and stops the script

0

ok fixed

0

Not "desperated", Deprecated.

0

"desperated"? You guys are making basic Lua look like rocket science...

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