Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to print out the players name?

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

✓ Accepted answer

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)

3 comments

0

Change it to print("player is dead:" .. Player.Name) in line 14

0

Thanks alot, this worked, and i will remember this

0

No probs

This discussion is preserved as it was. New answers, comments, and votes are closed.