Getting the player char from workspace but it got the player from Players instead, ethier tho is reference to workspace

So I’m making a NPC thing where a player hit a NPC with a rocket and the NPC will walk to the player, Here are the scripts(btw I just remove the codes that make the NPC walk to the player to make it shorter)

local script parented to a player tool

local player = game.Players.LocalPlayer
hit.Parent.Hitted:FireServer(game.Workspace:FindFirstChild(player.Name)

script parented to the NPC
local function FindHittedBy(Player)

	local target
	
	if Player ~= nil then
		print(Player.Parent)
	end
	
	return target
end

script.Parent.Hitted.OnServerEvent:Connect(function(Player)
	local torso = FindHittedBy(Player)
end)

for the function FindHittedBy, there is a print that print the player character parent and it prints Players which is the service Players but it should be workspace because in the remote event parameter we pass the player character.

I’ve been trying to fix this for a while but I can’t, I tryed to pass the player name as the parameter and make the script find the player character in workspace, there wasn’t any error for that but when I try to refernce the HumanoidRootPart, the HumanoidRootPart is nil(which I’m pretty sure that the object is the player from Players). So I’m just really confuse why the script is looking at the player from Players instead of workspace when I refernce it to the workspace.

What are you trying to do? You’re physically printing the parent of the player, which is game.Players. You’re also defining target but not putting anything, so it is nil… ?

I said that I remove the NPC pathfinding thing which was inlcuding target(I should of remove the target thing from the code block)

If you’re putting the player’s name as the parameter, then you’ll want to do

local function FindHit(player)
    if game.Players:FindFirstChild(player) then
        print("Player "..player.." exists!")
    end
end

Your function is pointless, you can simply print player

I’m trying to get the player character humanoidrootpart position in the workspace but the humanoidrootpart is nil(The humanoidrootpart does exist in the player character)

Okay, so your problem is with the parameters the server is getting. The player is always parsed when you fire the server.
So,

script.Parent.Hitted.OnServerEvent:Connect(function(player, Player)
	local torso = FindHittedBy(Player)
end)
1 Like

That make sense I’m so dumb lol, I forgot that the player is a automatic parameter(I’m gonna test this and see if it works)

Because you put the player argument in the wrong place. The server script receives the request from the client, the parameters are player who fired the event and the arguments. The player who fired the event argument is, of course, a Player object/

In your server script, it should be:

script.Parent.Hitted.OnServerEvent:Connect(function(playerWhoFiredTheEvent, Player)
	local torso = FindHittedBy(Player)
end)

For documentation, refer back to OnServerEvent.

1 Like

thanks guys it works perfectly