How to get the char from :GetPlayerFromCharacter?

Before you say that its stupid, I couldn’t figure out how to see if a target.hit was a player. And I asked for help and then this dude got really mad at me. He couldn’t explain why it worked and what I can do with it so I can understand it better.


In which always happens on discord (lol)

So I decided to make another post here. How do I get the character from a Target.Hit?

Heres my serverscript in case you need it:

local Players = game:GetService("Players")
local Tool = script.Parent
local ServerEvent = Tool:WaitForChild("OnFired")
ServerEvent.OnServerEvent:Connect(function(Player, MousePosition, Target)
	print(Target)
	if typeof(Target) == "Instance" and Players:GetPlayerFromCharacter(Target.Parent) then
		print(Target.Parent.Name)
	end
end)

anything helps.

Players:GetPlayerFromCharacter returns a Player Instance. Just do:

local Players = game:GetService("Players")
local Tool = script.Parent
local ServerEvent = Tool:WaitForChild("OnFired")
ServerEvent.OnServerEvent:Connect(function(Player, MousePosition, Target)
	print(Target)
    local FoundPlayer = Players:GetPlayerFromCharacter(Target.Parent) -- returns either  a Player or nil
	if typeof(Target) == "Instance" and FoundPlayer then
        local Character = FoundPlayer.Character -- the player's character
		print(Target.Parent.Name)
	end
end)
1 Like

Is the character not Target.Parent ?

1 Like

Is this a RayCast Hit? Im not getting the full picture here cause Im not seeing any Target.Hit

--//Services
local Players = game:GetService("Players")

--//Variables
local Tool = script.Parent
local ServerEvent = Tool:WaitForChild("OnFired")

--//Functions
ServerEvent.OnServerEvent:Connect(function(Player, MousePosition, Target)
	local TargetPlayer = Players:GetPlayerFromCharacter(Target.Parent)
	
	if TargetPlayer then
		local Character = TargetPlayer.Character
		
		print(TargetPlayer.Name, "is the target.")
	end
end)
1 Like

Target can be named anything, for example if you do

ServerEvent:FireServer(MousePos,Target)

it will get the mouse pos and the player target for the server sided when u call it SO when u get it you will see player just ignore it’s needed to get the actual player not the character, to get what you get from the client fire server its the same order just ignore the “Player” You can name it anything and it will get the MousePos and Target.

ServerEvent.OnServerEvent:Connect(function(Player, MousePositionRANDOMNAME, PLAYERTARGETMODEL)

end)

As you can see I renamed but it doesnt matter it will still get whatever the order is in the client.

Sorry if this aint explained well I hope someone else can explain better.

1 Like

Target.Parent was lined, would it still work?

the script gets the name of anything it clicks, not the parent (which I need the player, and the char)

I know it can, I just name it those cuz organization

If it’s underlined in Yellow, then it’s just a type error and won’t affect the script. If it’s underlined in Red then there’s an error somewhere

Then how are you using :GetPlayerFromCharacter() ?

I don’t know, the person who sent it to me didnt explain everything through. I should have rewritten it but I am super confused on getting the mouse’s position that i just need something that works

its orange. I think it affects something

FindFirstAncestorWhichIsA(“Model”) will tell you if the hit part is part of a model. If the model contains a humanoid and Players:GetPlayerFromCharacter(OtherModel) returns a player, it is a player character

		-- Finds if the touched part is part of a model
		--       For parts that are not part of a model, this will return as Workspace
		local OtherModel = OtherPart:FindFirstAncestorWhichIsA("Model")
		-- If the touched model exists and contains a humanoid (living creature)
		if OtherModel and OtherModel ~= game.Workspace and OtherModel:FindFirstChildWhichIsA("Humanoid") then
			-- If the hit model is a player character
			local Victim = Players:GetPlayerFromCharacter(OtherModel)
			if Victim then
				-- A player character has been hit
			end
		end

You raycasted the mouse’s position to get the target, right? I don’t understand why you can only get the name of the thing you’ve hit-

I am trying to damage the player but i need the character first, which i used hitcast to get the player, and i didnt know how, i asked a server but they werent any help they just gave me gibberish and I dont even know where i started

Well, maybe just try going into much more depth?

Ill try to find a tutorial on it instead. hopefully they have something useful.

Sorry if my posts aren’t ‘useful’ so far, I am just struggling to understand what you’re doing. You just gave us the server part, but we don’t know what you’re doing on the client…

yes sorry, i should have just searched it up anyway. I am still getting into depth of the advanced code.