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

? Can't always get all players in a function

I'm trying to make a server chat bot that anyone can use for help with the game. The problem is that it won't account for any players that join after it's ran, so it only works for the first person who joins. Can someone help me?

Here's my code:

local plrs = game.Players:GetPlayers()
for i = 1, #plrs do
plrs[i].Chatted:Connect(function(chat)
	if chat:sub(1, 1) == prefix then
	wait(0.2)
	
	-- Global cmds
	
	if chat:match(prefix.."help") then msg("filler")
	elseif chat:match(prefix.."test") then msg("Success")
	elseif chat:match(prefix.."send") then msg(string.sub(chat, 7))
	
	-- Admin cmds below
	
	elseif chat:match(prefix.."startmsg") then
		if v.Name ~= ownername then
			msg("Admin command only!")
		elseif v.Name == ownername then
			msg("Chat '-help' for server help!")
		end
	end
	end
end)
end

1 answer

playeradded

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(chat)
    if chat:sub(1, 1) == prefix then
    wait(0.2)

    -- Global cmds

    if chat:match(prefix.."help") then msg("filler")
    elseif chat:match(prefix.."test") then msg("Success")
    elseif chat:match(prefix.."send") then msg(string.sub(chat, 7))

    -- Admin cmds below

    elseif chat:match(prefix.."startmsg") then
        if v.Name ~= ownername then
            msg("Admin command only!")
        elseif v.Name == ownername then
            msg("Chat '-help' for server help!")
        end
    end
    end
end)
end)

4 comments

0

What if there's already people in the server?

0

That does not matter when the script first runs there should be no players in the server if you really want to make sure then just do for i,v in pairs(game.Players.GetPlayers()) do code here end)

0

So I have to put all of the code twice? One for playeradded and the other for that

0

no, just this script

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