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

if statement runs even if its false?

Asked by 5 years ago

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

Log in to vote
1
Answered by
Rheines 661 Moderation Voter
5 years ago
Edited 5 years ago

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
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 EnderPowerHD 98 — 5y
0
why did u downvote mine? Explain. WideSteal321 773 — 5y
0
I didn't. Rheines 661 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)

0
yes but its still the same problem on the player is no shirt but the if statement still runs and stops the script EnderPowerHD 98 — 5y
0
ok fixed WideSteal321 773 — 5y
0
Not "desperated", Deprecated. Zafirua 1348 — 5y
0
"desperated"? You guys are making basic Lua look like rocket science... KardashevScale 110 — 5y

Answer this question