Help on kill button?

I am making an admin UI.

I wan’t the admin to be able to write a persons name in a TextBox then click a TextButton to make that player reset. But it doesn’t work. The error says “PlayerName is not a valid member of Workspace” I get that the variable doesn’t work, but how do I fix it?

Script (KillScript) Click on the image.
Imgur

GUI in Explorer. Click on the image.
Imgur

1 Like

I don’t think you called the function, so the script does not know what to do. Also, you you need a remote event to fire the client to the server

I’d also suggest switching back to a monospace font like the default Courier New, this is so all the characters line up. It makes code significantly easier to read.

You can’t do workspace.PlayerName. Roblox thinks u want to access something ACTUALLY caled PlayerName. To fix this use workspace:FindFirstChild(PlayerName) or workspace[PlayerName] or in your usecase game.Players:GetPlayerFromName()

You have a number of problems and issues here:

  • Don’t send screenshots of your code: This doesn’t allow other developers to copy your code easily. We have codeblocks for this reason – check the markdown

  • Don’t use normal scripts for GUI manipulation: Why? Because what you do on the client will not be visible on the server in respect to FilteringEnabled. For all things of gui manipulation, use a local script

  • You’re defining a variable that is going to be the current text of the textbox and will remain constant when you think it would grab the current text of the textbox when you press the button. This is a common issue with people. You also can’t do game.Workspace.PlayerName because the script will assume that you want to find an instance named “PlayerName” in the workspace. You can do workspace[PlayerName] or workspace:FindFirstChild(PlayerName), but the best option is to find the player in the Players service using FindFirstChild and get their character.

  • You’d want to use RemoteEvents for this because if you just kill another player on your client, they’ll break on your client, but on the server and other clients, they’ll be fine

  • This should be the updated script:

-- Local script inside the TextButton
local RE = game.ReplicatedStorage.RemoteEvent

function KillPlayer()
    local player = game.Players:FindFirstChild(script.Parent.Parent.PlayerText.Text)
    RE:FireServer(player)
end

script.Parent.MouseButton1Click:Connect(KillPlayer)

IMPORTANT NOTE: You need to check if the player who fired the event is an admin, because exploiters can fire this event without needing admin and kill other players (this is what the checkIfAdmin function is for)

-- Normal script inside ServerScriptService

local RE = game.ReplicatedStorage.RemoteEvent

RE.OnServerEvent:Connect(function(admin, target)
    if checkIfAdmin(admin) then -- for security reasons, check if the player for fired the event is an admin
        local char = target.Character
        if char then
            char.Humanoid.Health = 0
        end
    end
end)

@LukaDev_0 GetPlayerFromName is not a thing

1 Like