GetPlayerFromCharacter is unstable / not working

Hello devs,

Hope all is well. I’m currently having an issue with GetPlayerFromCharacter not functioning properly. Sometimes it works, sometimes it doesn’t, I’m not sure what to do. Can anyone help me, please? Thanks.

(Checks if player is found)

-- Variables

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

touchPart.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = Players:GetPlayerFromCharacter(character)
	
	if player then
		print(player.Name," is detected")
	else
		print("player is not detected")
	end
end)

Alternate version

-- Variables

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


touchPart.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = Players:GetPlayerFromCharacter(character)
	
	print(player.Name)
end)

Maybe touch is unanchored. A walking character might cause tiny changes in its position, triggering the event.

To debug, print(hit.Name) in place of print("player is not detected").

1 Like

Im assuming that the touch part is on top of the white part, which would mean that things like the hat and double blades on your characters back would touch the part, but their parent isn’t your character

1 Like

image
Wouldn’t this be considered in the character?

I’m interested in knowing what hit is in these erroring lines. You can use the first snippet and replace the print where the player was not found with print(hit.Name).

Edit. Refer to Dragon_bloxy, they’re right, handles.

1 Like

you misunderstand the accessory has a mesh part, named “Handel”, inside of it which is the actual part that handles the physics

1 Like

like this:
image

1 Like

Yes, I have anchored the part.

Here’s the result:

Oh my bad! It could be… do you recommend anything to combat that, if it is the issue?

i mean as long as you just check that the player exists with an if statement then run your code inside that, you should be good

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.