How to determine if a player touches a part, then a value inside the player changes

Ive been having troubles trying to script this in, all i’m getting is errors so now im going ask the community. Heres my script im trying to make the BoolValue called Hiding true inside a player:


local Players = game:GetService("Players")

local part = script.Parent

local function onTouched(part)

local player = Players:GetPlayerFromCharacter(part.Parent)

if not player then return end

print(player.Name .. "IS hiding")

part.Parent:WaitForChild("Hiding").Value = true

end

part.Touched:Connect(onTouched)

image

but it wont work, any ideas?

Should the Hiding BoolValue be parented to the part that you connected the Touched event to?

You’ve defined two variables part, first to get the script’s parent and second in the onTouched function, which will override the part variable in the scope of the function. part in the onTouched function will be the part of the player that touched your original part, which is why Hiding is not being found.

Rename your onTouched variable to something else, such as hit, to avoid overriding your variable.

local Players = game:GetService("Players")
local part = script.Parent

local function onTouched(hit)
    local player = Players:GetPlayerFromCharacter(hit.Parent)
    if not player then return end

    print(player.Name .. "IS hiding")

    part.Parent:WaitForChild("Hiding").Value = true
end

part.Touched:Connect(onTouched)

Thanks I tried it but i get this error: Infinite yield possible on 'Workspace.SecretBaseLocker:WaitForChild(“Hiding”)