How would I go about killing a player whos username was put into a textbox and then a button was pressed?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to make an Admin GUI with the ability to kill the player.

  2. What is the issue? The script thinks the textbox has no text. (it does)

  3. What solutions have you tried so far? I have tried to check if it has changed.

My code is below:

script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Parent.TextBox.Text == "" then
		print("Nothing.")
	else
		workspace[script.Parent.Parent.TextBox.Text].Head:Destroy()
	end	
end)

I am hoping you can help me out!

There are a few issues with this code. Firstly, if you were to kill the subject from the admins client, nothing would happen as for it to be replicated (shown) to other players, you’ll have to do it from the server. To make it happen from the server, you’ll have to use a RemoteEvent, which you can find more about here

Secondly, workspace[directory] would only work if the players name was spelt correctly, and if it wasn’t it’d error. This is, however, a pretty inefficient way to kill the player and there are better ways of doing as such, like setting their health to 0, deleting their character, etc.

If you take this into consideration, your local script (client one) and script (server) should look like this:

-- Local script
local remote = REMOTE_DIRECTORY_HERE
local button = script.Parent

button.MouseButton1Click:Connect(function()
   local text = button.Parent.TextBox.Text -- For ease of use/ access
   if text == "" then
      print("nothing")
   else
      local subject = game.Players:FindFirstChild(text) -- Sees if theres a player
      if not subject then return false end -- if a player isn't found then end

      remote:FireServer(subject)
   end
end)
-- Server Script here
local remote = REMOTE_DIRECTORY_HERE

remote.OnServerEvent:Connect(function(plr, subject)

   -- Any sort of sanity checks should be done here
   local subChar = subject.Character or subject.CharacterAdded:Wait()
   local subHum = subChar:FindFirstChild("Humanoid")

   -- Now we can use any sort of killing method
   subHum.Health = 0
   subHum:Kill()
   subChar:Destroy()
 end)

Your code detects if the text is a = “” from the start ,so put your code in a while true do loop with a wait(0.01) or something

The method they were previously using works fine, if anything using a while loop for this sort of situation would not only be inefficient due to polling but also cause errors and issues which can easily be avoided by using an event, as they have done now.

1 Like

When you insert the name of the player from an output, take the name of the player and put it into a stringvalue value. then you can do the following: (Assuming this is a local script)


local killplayer = stringvalue.value

script.Parent.MouseButton1Click:Connect(function(e)
game.players:FindFirstChild(killplayer).Character.humanoid.Health = 0
end)


since you are using a textbox text to find the player you can also change
“local killplayer” to


local killplayer = Textbox.text


to find and select which player you want, you can find that on your own because i don’t know any way to do that

I made a similar video to it a few months back. Just make sure instead of the player:Kick(), fire a remote event and kill the player.

Edit:

It isn’t a good way to kill the player. Just simply set the Humanoid’s health to 0; Humanoid.Health = 0