Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I get a player's character and humanoid from a server-side script?

Asked by 2 years ago

I've searched through so many devforum posts yet I have not found an answer. I need to find the character to get the humanoid so when a player activates an admin command to kill a player, the player stated is killed. Here's my script.

Once again, it's a server script.

commands.kill = function(sender, arguments)

    print("Kill command fired by "..sender.Name)
    local plrToKill = arguments[1]
    if plrToKill == "me" then
        plrToKill = string.lower(sender.Name)
        arguments[1] = plrToKill
    end
    local plrChar = game.Workspace:FindFirstChild(plrToKill)

    if plrToKill then
        local plr = findPlayer(plrToKill)

        if plr then
            local plrHum = nil
            local health = nil
            print(plrChar)

            plrHum = plrChar.Humanoid
            print(plrHum)

            health = plrHum.Health

            print(health)

            health = health - 100
        end
    end
end

Everything else works fine, but I get an error saying "attempt to index nil with 'Humanoid' which I find very confusing. Any help is greatly appreciated.

0
Print ''arguments[1]'' and tell me the result, I think I know the issue Cirillix 110 — 2y
0
If 'arguments[1]' is "me", then you've changed the username to all lowercase. Clearly, your username will not match that Ziffixture 6913 — 2y
0
Furthermore, you're trying to make a reference to 'Humanoid.Health', which is something you cannot do; adjusting 'Health' will only adjust the variable Ziffixture 6913 — 2y
0
When you refer to a property, you read from it. Ziffixture 6913 — 2y
View all comments (3 more)
0
And why not just set 'Humanoid.Health' to zero? Ziffixture 6913 — 2y
0
And why search Workspace when you can just look into Player's directly? Ziffixture 6913 — 2y
0
You can use a remoteevent on the server to get the player and players character. Oliver_Bocch 32 — 2y

Answer this question