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)
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)