You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Make a gui button that kills the player thats name was typed in the TextBox.
What is the issue? Include screenshots / videos if possible!
It brings up the debugger and gives me a error ( attempt to index nil with ‘Humanoid’
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Many, and yes.
Code:
function onclick()
local salad = game.Workspace:FindFirstChild(script.Parent.Parent.UserBox.Text)
salad.Humanoid.Health = 0
end
script.Parent.MouseButton1Click:connect(onclick)
local button = script.Parent-- button object here
local box = script.Parent.Parent.UserBox -- textbox object here
button.MouseButton1Down:Connect(function()
local boxTxt = string.lower(box.Text)
local Player = nil
for i,v in pairs(game.Players:GetPlayers()) do
if string.lower(v.Name) == boxTxt or string.lower(v.DisplayName) == boxTxt then
Player = v
break
end
end
if Player == nil then
warn("no player was found")
else
warn("Player: " .. Player.Name)
Player.Character.Humanoid.Health = 0
end
end)
What this script does is
Fire event for button
Gets the text of the textbox and converts it to lowercase
Loops through all the players
Checks if the lowercase username is equal to the lowercase box text or the lowercase displayname is equal to the box text
why do this?
setting both name and boxtext to lowercase makes it not rely on caps
it works for both display and usernames
An alternative is just doing
game.Players:FindFirstChild(box.Text)
however it relies on whether the capitalization is perfect and it doesnt work with display names.
My guess is that’s a ServerScript. If that’s the case, the reason it’s not working is because the Text in the Textbox does not replicate between client and server. The client is inputting the Text but when the server goes to read it, because it doesn’t replicate, the server still sees it as empty (nil).
nil does not have a humanoid and that’s why it will output attempt to index nil with 'Humanoid'.
To get around this obstacle I would use RemoteEvents. I would put this as a LocalScript:
function onclick()
-- Change RemoteEvent to your remote event.
RemoteEvent:FireServer(script.Parent.Parent.UserBox.Text)
end
script.Parent.MouseButton1Click:Connect(onclick)
Then on a ServerScript:
function KillPlayer(playerwhoclicked, player)
local character = game:GetService("Workspace"):FindFirstChild(player)
character:FindFirstChild("Humanoid").Health = 0
end
-- Change RemoteEvent to the same remote event used in the LocalScript
RemoteEvent.OnServerEvent:Connect(KillPlayer)
For something like this you will want to check (on the ServerScript) if the player who fired the remote, is allowed to. For example:
function KillPlayer(playerwhoclicked, player)
if playerwhoclicked.UserId == 3715871599 then
local character = game:GetService("Workspace"):FindFirstChild(player)
character:FindFirstChild("Humanoid").Health = 0
end
end
Checking if the player is allowed will stop exploiters from using these remotes to their advantage.
Feel free to ask any questions if you are confused.
Oh I did make a pretty dumb mistake. Yes you need to use a remote event, however you didn’t really explain the best.
Here is a revision of my code that works:
local script
local button = script.Parent-- button object here
local box = script.Parent.Parent.UserBox -- textbox object here
button.MouseButton1Down:Connect(function()
local remote = game.ReplicatedStorage.RemoteEvent -- you need to create a remote event in replicated storage.
local boxTxt = string.lower(box.Text)
local Player = nil
for i,v in pairs(game.Players:GetPlayers()) do
if string.lower(v.Name) == boxTxt or string.lower(v.DisplayName) == boxTxt then
Player = v
break
end
end
if Player == nil then
warn("no player was found")
else
remote:FireServer(Player)
end
end)
it should be noted that this remote event is not secured for admins. If you want to secure it for admins do
local admins = {} -- add admin userIds here
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(plr, victim)
if table.find(admins, plr.UserId) then
victim.Character.Humanoid.Health = 0
end
end)