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

Why won't this script work? Attempt to index nil with 'Name'?

Hello. I was trying to make a script that would create a message whenever a new player joins the game, but it seems to not work. The output shows this error: "Workspace.Welcome Note:5: attempt to index nil with 'Name' ". I have no idea on how to fix this. Help?

local Players = game:GetService("Players")

function onPlayerAdded(player)
	local message = Instance.new("Message", game.Workspace)
	message.Text = player.Name.. " has entered the game" --Error here.
	wait(5) 
	message.Parent = nil
end

Players.ChildAdded:Connect(onPlayerAdded())

1 answer

✓ Accepted answer

A couple of mistakes.

  1. You should be using PlayerAdded not ChildAdded.

  2. You need to remove the parenthesis in the Connect signal.

I made some other improvements too.

local Players = game:GetService("Players")

function onPlayerAdded(player)
    local message = Instance.new("Message", workspace) -- Roblox has a workspace global
    message.Text = player.Name.. " has entered the game"
    wait(5) 
    message:Destroy() -- lets just destroy it instead
end

Players.PlayerAdded:Connect(onPlayerAdded) -- we can use the PlayerAdded event for when a new player joins the server

Be sure to accept this answer if this helps!

1 comment

1

Thank you, Tyler!

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