Help with checking if a character in the workspace with the same name as a player is a player

Ok so I’m making an admin panel right now for lots of games that my community and friends are making and I’m looking to add a “kill” option. This will kill the player who’s name was typed in the textbox upon the press of a button. I want to check if the player who’s name was typed was actually a player or just an object with the same name in the workspace, how do I do this?

local event = script.Parent.KillEvent

event.OnServerEvent:Connect(function(plr, name)
	local foundplayer = workspace:FindFirstChild(name)
	local plr = game.Players:GetPlayerFromCharacter(foundplayer)
	if plr ~= nil then
		plr.Character.Humanoid.Health = 0
	end
end)
3 Likes

Just use if workspace[name]:FindFirstChild("Humanoid") then to check if the object has a humanoid. If it has, then it is guaranteed that it is a real character. If there is any issue, reply.

2 Likes

what if the developer of the game put their own character into the workspace for whatever reason with humanoid still inside?

1 Like

You can just add some checks and you’re fine:

event.OnServerEvent:Connect(function(plr, name)
	local foundchar = workspace:FindFirstChild(name)
if foundchar then -- if a character is found
	local foundplr = game.Players:GetPlayerFromCharacter(foundchar) 
	if foundplr ~= nil then
		foundplr.Character.Humanoid.Health = 0
	end
end
end)

You can add an object into the unwanted character and check in the script if the actual character does not have that object.

How would I go about doing this automatically for any unwanted character in the workspace in every game this admin panel is inserted into? (I edited this as I think I responded with the wrong idea in mind last time)

I thought this would be the solution as the coding here looks flawless but its still not working and I have absolutely no idea why.

There are no console errors either.

local event = script.Parent.KillEvent

event.OnServerEvent:Connect(function(plr, name)
	local foundchar = workspace:FindFirstChild(name)
	if foundchar then -- if a character is found
		local foundplr = game.Players:GetPlayerFromCharacter(foundchar) 
		if foundplr ~= nil then
			foundplr.Character.Humanoid.Health = 0
		end
	end
end)

It’s possibly the script that’s firing the remote, can you show that? I don’t think the server script is the problem here

local button = script.Parent
local textbox = script.Parent.Parent.CMDBAR
local event = script.Parent.KillEvent

button.MouseButton1Click:Connect(function()
	local name = textbox.Text
    event:FireServer(name)
end)

(This is the local script in question)

This should need to work.

local event = script.Parent.KillEvent

event.OnServerEvent:Connect(function(plr, name)
	local foundchar = game.Players:FindFirstChild(name).Character
	if foundchar then -- if a character is found
		local foundplr = game.Players:GetPlayerFromCharacter(foundchar) 
		if foundplr ~= nil and foundchar~=nil then
			foundchar.Humanoid.Health = 0
		end
	end
end)

WOW thank you so much! This works perfectly! This will be extremely helpful to my admin panel! I will be crediting you in the credits section.

No problem, I just like to help people.

Wait, no I’m such an idiot man. It actually doesn’t work :frowning:. I tested with my friend just now and it still killed me. I have no idea what to do and I’m so sorry for thinking you solved it. I need to test things more.

Hm… Do u mind if i remake the system?
Also, Does it require the full username name?

no, I don’t mind at all and yes it does. If you want I can publish the model and give it to you, do you need that or screenshots? just tell me what you need.

No nothing is needed, I have actually made my own admin panel before.
So this remake should need to be relatively easy.

wait… where is the KillEvent event even located?

oh wow that sounds amazing! thank you so much again for helping me, and I apologise for the confusion

image