How do I access the character in the Playerservice from a .touched event?

So what I have been trying to do lately is access the Player’s character from the model in workspace when a part is touched.
Basically this:

script.Parent.touched:Connect(function(hit)
end)

So I tried looking for solutions on how to access the character in the Player service, however I did not come across any. Only to access the model in workspace from the character.

Has anybody got the solution to this? I’m sure this is something basic that I might have missed. I’m fairly new to scripting.

Thanks in advance! :smiley:

check if the hit.Parent is a model and has humanoid in it

if hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") then
   local char = hit.Parent 
   --REST OF THE CODE
end
1 Like
Part.Touched:Connect(function(Hit)
	local Model = Hit:FindFirstAncestorWhichIsA("Model")
	local Humanoid = Model:FindFirstChildWhichIsA("Humanoid")
	
	if Model and Humanoid then
		...
	end
end)
1 Like

Thanks for the fast reply! How would I be able to access the character itself then? Like for example to change things in the leaderboard?

so get access to the player?
you add a line
local player = game.Players:GetPlayerFromCharacter(char)
and you do what you want with it

1 Like
script.Parent.touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
if humanoid then
local character = humanoid.Parent
local player = game.Players:GetPlayerFromCharacter(character)
end
end)
1 Like

Alright, I get it. Thank you so much!

1 Like

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