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

How to print out the players name?

Asked by 2 years ago
Edited 2 years ago

Hello everyone, this is my second post here, and i have another question How do i print out the players name, yes i know this is pretty easy, but i tried some methods, it did not work, it always said: attempt to index nil with Name heres the script, i made a lava-like part, that kills you, and will print out the victims name

local lava = script.Parent -- making the lava variable
local players = game:GetService("Players")
local player = game.Players.LocalPlayer

local function killPlayer(otherPart) --the otherPart is what the object got touched with, a player touched the object with right foot,and then the script checks if its from a humanoid
    local partParent = otherPart.Parent
    local human = partParent:FindFirstChild("Humanoid")
    human = partParent:WaitForChild("Humanoid")
        if human.Health == 0 then
            print("player is dead:", player.Name)
        end
        if human then
            human.Health = 0
        end
    end

lava.Touched:Connect(killPlayer)

(you might also tip me some scripting tips)

1 answer

Log in to vote
0
Answered by 2 years ago

You cannot get the local player using a server script. We can use GetPlayerFromCharacter for this!

local lava = script.Parent
local players = game:GetService("Players")

function KillPlayer(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local Player = players:GetPlayerFromCharacter(Hit.Parent)

        if Player then
            local Humanoid = Hit.Parent:FindFirstChild("Humanoid")

            if Humanoid.Health > 0 then
                Humanoid.Health = 0
            else
                print("player is dead:" .. Player.Name")
            end
        end
    end
end

lava.Touched:Connect(KillPlayer)
0
Change it to print("player is dead:" .. Player.Name) in line 14 SEAN_YT213 139 — 2y
0
Thanks alot, this worked, and i will remember this Artemiygogolev 23 — 2y
0
No probs SEAN_YT213 139 — 2y
Ad

Answer this question