Script wont detect chat commands at all

Code:

repeat task.wait() until #game.Players:GetPlayers() > 0

local LotteryTiers = {
	1;
};

local LotteryStatus = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents"):WaitForChild("LotteryStatus")

local Prefix = "/"

local ChosenLotteryTier

local ChosenNum1

local ChosenNum2

local ChosenNum3

local ChosenNum4

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.Chatted:Connect(function(msg)

		msg = string.lower(msg)

		local args = string.split(msg, " ") -- /lottery 10 20 3
		
		if args[1] == Prefix .. "lottery" then
			
			print("123")
			
		end

	end)

end)

--LotteryStatus:FireAllClients(ChosenLotteryTier, "Start")

local Winners = {}

while true do

	ChosenLotteryTier = LotteryTiers[math.random(1,#LotteryTiers)]

	LotteryStatus:FireAllClients(ChosenLotteryTier, "Start")

	if ChosenLotteryTier == 1 then

		ChosenNum1 = math.random(1,4)

		-- 25% total chance

	elseif ChosenLotteryTier == 2 then

		ChosenNum1 = math.random(1,5)

		ChosenNum2 = math.random(1,5)

		-- 10% total chance

	elseif ChosenLotteryTier == 3 then

		ChosenNum1 = math.random(1,10)

		ChosenNum2 = math.random(1,10)

		ChosenNum3 = math.random(1,10)

		-- 3.33% total chance

	elseif ChosenLotteryTier == 4 then

		ChosenNum1 = math.random(1,20)

		ChosenNum2 = math.random(1,20)

		ChosenNum3 = math.random(1,20)

		ChosenNum4 = math.random(1,20)

		-- 1.25% total chance

	end

	task.wait(10)

end

Since you have

repeat task.wait() until #game.Players:GetPlayers() > 0

at the beginning, you would have already joined the server by the time your script got to the PlayerAdded event; therefore, there would be no chatted event for the first player that joined.

1 Like

Get rid of the loop at the start. You’re preventing the code from detecting the first person who joined.

Code:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local LotteryStatus = ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("LotteryStatus")

--//Controls
local Prefix = "/"
local ChosenLotteryTier = nil
local ChosenNum1 = nil
local ChosenNum2 = nil
local ChosenNum3 = nil
local ChosenNum4 = nil

--//Tables
local Winners = {}
local LotteryTiers = {
	1
}

--//Functions
Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		msg = string.lower(msg)

		local args = string.split(msg, " ") -- /lottery 10 20 3

		if args[1] == Prefix .. "lottery" then
			print("123")
		end
	end)
end)

--LotteryStatus:FireAllClients(ChosenLotteryTier, "Start")

while true do
	ChosenLotteryTier = LotteryTiers[math.random(#LotteryTiers)]
	LotteryStatus:FireAllClients(ChosenLotteryTier, "Start")

	if ChosenLotteryTier == 1 then
		ChosenNum1 = math.random(4)

		-- 25% total chance

	elseif ChosenLotteryTier == 2 then
		ChosenNum1 = math.random(5)
		ChosenNum2 = math.random(5)

		-- 10% total chance

	elseif ChosenLotteryTier == 3 then
		ChosenNum1 = math.random(10)
		ChosenNum2 = math.random(10)
		ChosenNum3 = math.random(10)

		-- 3.33% total chance

	elseif ChosenLotteryTier == 4 then
		ChosenNum1 = math.random(20)
		ChosenNum2 = math.random(20)
		ChosenNum3 = math.random(20)
		ChosenNum4 = math.random(20)

		-- 1.25% total chance
	end

	task.wait(10)
end

If you’re doing math.random(1, number), you can just do math.random(number) because it automatically picks a number between 1 and number.

1 Like