Closest player find with a input, BIG PROBLEM

Hey

when I press Q it should find the nearest player. My problem is that it just print out:

‘ServerScriptService.Script.nearestplayer’, Line 5 - function ClosestHumanoid - Studio - nearestplayer:5
Script ‘ServerScriptService.Script’, Line 3

So I cant seriously define why this doesnt work

here is the module script:

local module = {}
module.__index = module

function module.ClosestHumanoid()
	local npc_directory = game.Workspac.npc_directory
	
	local player = game.Players:GetPlayers();
	local npcs = npc_directory:GetChildren();
	
	
	local closest = { humanoid = false, distance = math.huge } 
	local character = player.Character or player.CharacterAdded:wait();
	local root = character:WaitForChild('HumanoidRootPart', 1);

	local to_check = npcs;
	for _,PLAYER in pairs(player) do
		if PLAYER.Character and PLAYER ~= player then
			table.insert(to_check, PLAYER.Character)
		end
	end

	for _,humanoid_model in pairs(to_check) do
		local displacement = (humanoid_model .HumanoidRootPart.Position - root.Position).magnitude;
		if displacement < closest.distance then
			closest.distance = displacement;
			closest.humanoid = humanoid_model:FindFirstChild('Humanoid');
		end
	end
	
	setmetatable(closest, module)
	return closest.humanoid
end



return module

Here is the local script:

local uis = game:GetService("UserInputService")
local works = game:GetService("ReplicatedStorage").works

uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Q then
works:FireServer()
end
end)

Here is the script:

local works = game:GetService("ReplicatedStorage").works
local nearestplayer = require(script.nearestplayer)
local closesthumanoid = nearestplayer.ClosestHumanoid()
works.OnServerEvent:Connect(function(player)
if closesthumanoid then
print("it does work")
end
end)

And if you want the data then here is it:
nearestplayer.rbxl (23.6 KB)

You wrote this wrong

local npc_directory = game.Workspace.npc_directory
1 Like

I dont know. It still doesnt work:

13:55:04.679 Stack Begin - Studio
3:55:04.679 Script ‘ServerScriptService.Script.nearestplayer’, Line 12 - function ClosestHumanoid - Studio - nearestplayer:12
13:55:04.679 Script ‘ServerScriptService.Script’, Line 3 - Studio - Script:3
13:55:04.680 Stack End - Studio

You’re trying to get the Character from the players service, you need to change your code accustom checking each character

your getting all the players in a table

local character = player.Character or player.CharacterAdded:wait();

your trying to do .Character at the wrong thing

okey and how can I fix this whole script?

Should I put it in a local script? instead of using it in serverscriptservice?

Does the local player only need to care about the closest humanoid? If so, you should put it in a localscript and change up the code to make it work for a localscript instead of a modulescript

local uis = game:GetService("UserInputService")

local function findclosestplayer()
	local npc_directory = game.Workspace.npc_directory

	local player = game:GetService("Players").LocalPlayer
	local npcs = npc_directory:GetChildren();


	local closest = { humanoid = false, distance = math.huge } 
	local character = player.Character or player.CharacterAdded:wait();
	local root = character:WaitForChild('HumanoidRootPart', 1);

	local to_check = npcs;
	for _,PLAYER in pairs(player) do
		if PLAYER.Character and PLAYER ~= player then
			table.insert(to_check, PLAYER.Character)
		end
	end

	for _,humanoid_model in pairs(to_check) do
		local displacement = (humanoid_model .HumanoidRootPart.Position - root.Position).magnitude;
		if displacement < closest.distance then
			closest.distance = displacement;
			closest.humanoid = humanoid_model:FindFirstChild('Humanoid');
		end
	end

	return closest.humanoid
end

local closestplayer = findclosestplayer()

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		if closestplayer then
			print("it does work")
		end
	end
end)

Still doesnt work. I am trying everything and it does not work ://

Firstly, shouldn’t

local closestplayer = findclosestplayer()

Be inside of if statement if the KeyCode is Q so it gets latest information?

Secondly,

local player = game:GetService("Players").LocalPlayer

In your other pieces of code in the function, it expects this varaible to be a table of players. Trying making a another variable with all of the players i nthe game and use that in your

for _,PLAYER in pairs(player) do
		if PLAYER.Character and PLAYER ~= player then
			table.insert(to_check, PLAYER.Character)
		end
	end

loop

1 Like

and how can I fix this now that sounds like a lot, and I dont think I am able to do this :/?

Basically, i nyour function, create a new variable that has all the players that are in the game

local players = game.Players:GetPlayers()

then in

for _,PLAYER in pairs(player) do
		if PLAYER.Character and PLAYER ~= player then
			table.insert(to_check, PLAYER.Character)
		end
	end

Change pairs(player) to pairs(players)` since again, it expects a table

That’s all for the function, now for the external parts

local closestplayer = findclosestplayer()

I recommend you put this in your InputBegan code since you want latest data


uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
        local closestplayer = findclosestplayer()
		if closestplayer then
			print("it does work")
		end
	end
end)

Edit: What I mean by that is you first make a variable in your function that contains every player in the game via game.Players:GetPlayers() and then in you edit the loop to use that variable. Say you named it players, change the loop to

for _,PLAYER in pairs(players) do
	if PLAYER.Character and PLAYER ~= player then
		table.insert(to_check, PLAYER.Character)
	end
end

for _,PLAYER in pairs(player) do
if PLAYER.Character and PLAYER ~= player then
table.insert(to_check, PLAYER.Character)
end
end

what do you mean with that I cant understand seriously. Cant you just edit what you mean then I can understand better?

1 Like