✓ Accepted answer
the proper way to get a player from a touched event is with the :GetPlayerFromCharacter() function. Ex.
workspace.TouchedEventPart.Touched:Connect(function(hit)
if hit and hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then
workspace.Baseplate.BrickColor = BrickColor.new("Sea green")
end
end)
This checks if hit exists, and the part that hit the part has a parent, which if a player touched it, should be a body part most of the time. the parent of body parts should always be the player character so then you get player from that character.
Note: if you also want to include NPCs in the category of entities that can trigger the event use:
workspace.TouchedEventPart.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
workspace.Baseplate.BrickColor = BrickColor.new("Sea green")
end
end)
if you only want players to trigger the event, it is encouraged to use :GetPlayerFromCharacter() as opposed to hit.Parent:FindFirstChild("Humanoid")
2 comments
Thank you.
np