Search code examples
roblox

Cannot get Roblox player attribute value


script.Parent.Touched:Connect(function(player)
    print("TOUCHED")
    if player:GetAttribute("Hiding") then
        print("tag player touch????")
    else
        print("HMM")

    end
end)

This should detect if a player, not a character, has the tag. I KNOW that the player has the tag.


Solution

  • When the .Touched event fires, it sends the workspace part that caused the collision as an argument to your custom .Touched event handler. You are calling GetAttribute on this part, which is contained inside the character. Instead, go up the parent heirarchy ( touchedPart.Parent.Parent... ) until you arrive at the character model.

    To find the player from the character, use

    local PlayerService = game:GetService("Players")
    local player = PlayerService:GetPlayerFromCharacter( character )
    

    You can find more information here: https://create.roblox.com/docs/building-and-visuals/physics/detecting-collisions