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

What did i do wrong with this script?

when a player enters the message appears

--Name announcer
winner = {"Player's name"}
function OnPlayerEntered(player)
if player.name == winnerthen -- is this correct?
local msg = Instance.new("Message")
msg.Parent = game.Workspace
msg.Text = winner "is the winner"
 wait(2)
msg:remove()
end
end
game.Players.ChildAdded:connect(OnPlayerEntered)

2 answers

This should be fixed, you had a lot of syntax errors.

--Name announcer
winner = "Player's name"
function OnPlayerEntered(player)
if player.Name == winnerthen -- is this correct?
local msg = Instance.new("Message")
msg.Parent = game.Workspace
msg.Text = winner.." is the winner"
 wait(2)
msg:remove()
end
end
game.Players.ChildAdded:connect(OnPlayerEntered)

I see a few errors, but they can be fixed. Here, lets fix those;

--Name announcer
winner = {"Player's name"} --This is where the Player's name will go
function OnPlayerEntered(player) --This seems correct
if player.name == winner then -- is this correct? Nope :P
local msg = Instance.new("Message") --This is correct
msg.Parent = game.Workspace --This is correct
msg.Text = winner "is the winner" --Something is wrong here :P
wait(2) --Our wait, thats correct
msg:remove() --':remove()'?
end
end --You got the 'end's correctly
game.Players.ChildAdded:connect(OnPlayerEntered) --Ok, I guess? :P

Now, lets fix up the code;

--Name announcer
winner = {"Player's name"} --Ok, heres our 'table' that is currently containing the Names of users

local function ChkPlr(plr,str) --A new function; this will be used to check if the the Player is in the 'winner' table/list
for i = 1, #winner do if plr:lower() == winner[i]:lower() then return true end end --The four loop will go through every string in 'winner', and if the Player's Name matches to the list, then it'll return the Player true, else, it'll return the Player false
return false --This will fire if the Player is not in the list
end

function OnPlayerEntered(player)
if ChkPlr(plr.Name,false) then --Ah, doesn't this look a bit better? :P
local msg = Instance.new("Message")
msg.Parent = game.Workspace
msg.Text = winner.." is the winner" --You forgot the '..' for the string
wait(2)
msg:Destroy() --Why not use ':Destroy()' instead? :P
end
end
game.Players.ChildAdded:connect(OnPlayerEntered)

Hope this helped!

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