Script not working when trying not to find something?

So I have this script that should fire a remote event if the player does not have an item in the character, but it does not fire and gives the error of the player. of course not having it, any help?

> local Player = game.Players.LocalPlayer
> 
> script.Parent.Activated:connect(function()
> 
> if Player.Character.MaskForHide == false then
> 
> script.Parent.GiveMask:FireServer()
> 
> else
> 
> if Player.Character:WaitForChild("MaskForHide") then
> 
> script.Parent.RemoveMask:FireServer()
> 
> end
> 
> end
> 
> end)

WaitForChild should be FindFirstChild

Thats not the problem here???

WaitForChild will error after 5 seconds of the Instance not being found.

I know but thats not the error, the error is it saying “MaskForHide is not a valid memeber of model” on the

if not Player.Character.MaskForHide then part.

Which is what I want, but it does not fire the givemask.

Use FindFirstChild because you are indexing a nil value

What about when removing it? its not working.

try this

if not Player.Character:FindFirstChild("MaskForHide") then

Huh? No it won’t. WaitForChild will infinitely yield until the object is found if you don’t specify a timeout value or return nil if you specify a timeout value and the timeout is reached before finding an object.


Problem is:

MaskForHide is not a valid member of Model and there’s no object that allows you to shorthand like this. What you should be doing here is determining if a hat exists.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

script.Parent.Activated:Connect(function ()
    if Character then
        local hasMask = Character:FindFirstChild("MaskForHide")
        if hasMask then
            script.Parent.RemoveMask:FireServer()
        else
            script.Parent.GiveMask:FireServer()
        end
    end
end)

A note on the above: you could just use one remote and pass a boolean to determine whether it should be removed or not, instead of creating two separate remotes.

script.Parent.MaskEquip:Fire(hasMask)
-- On server: if hasMask, destroy, otherwise give