Why do both nextbots spawn?

Hi! I’m making a nextbot game, where when you say a certain word, a certain nextbot spawns. I have an issue where if a player says anything, both nextbots spawn at once and I don’t know why. Here are both codes:

Cop Script

local nextbots = game:GetService("ServerStorage"):WaitForChild("Nextbots")

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg == "cop" or "Cop" or "Police" or "Popo" or "911" or "Officer" or "Cadet" or "Sergeant" then
			local cop = nextbots:WaitForChild("Cop"):Clone()
			cop.Parent = workspace
			else end
	end)
end)

Rock Script

local nextbots = game:GetService("ServerStorage"):WaitForChild("Nextbots")

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg == "Rock" or "Dwayne Johnson" or "The Rock" or "Dwayned the Rock Johnson" then
			local rock = nextbots:WaitForChild("Rock"):Clone()
			rock.Parent = workspace
			else end
	end)
end)

You’re saying
if "Cop" then
if you ignore the first or.

You need to include msg == .

That is not the correct use of the keyword or.

You have to do it like this:

if msg == "Rock" or msg == "Dwayne Johnson" --[[etc]] then

end
1 Like

I guess he should use a table then…
Something like this:
Even though I haven’t used tables in a while, so I might have forgotten.

local serverStorage = game:GetService("ServerStorage")

local nextbots = serverStorage.Nextbots
local list = {"cop", "Cop", "Police", "Popo", "911", "Officer", "Cadet", "Sergeant"}

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        if message == list then
            nextbots.Cop:Clone().Parent = workspace
        else
            print("Couldn't find a word within the table list.")
        end
    end)
end)

If “if message == list then” is incorrect, then somebody can then correct me.

1 Like

You need a For loop to index the table if you use that method:

local nextbots = game:GetService("ServerStorage"):WaitForChild("Nextbots")
copTable = { "cop","police","popo","911","officer","cadet","sergeant" }
game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		msg = string.lower(msg) --make the message lowercase
		for i=1,#copTable do
			if string.match(copTable[i],msg) then
				local cop = nextbots:WaitForChild("Cop"):Clone()
				cop.Parent = workspace
				break
			end
		end
	end)
end)



local nextbots = game:GetService("ServerStorage"):WaitForChild("Nextbots")
rockTable = { "rock","dwayne johnson","the rock","dwayned the rock johnson" }
game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		msg = string.lower(msg) --make the message lowercase
		for i=1,#rockTable do
			if string.match(rockTable[i],msg) then
				local cop = nextbots:WaitForChild("Cop"):Clone()
				cop.Parent = workspace
				break
			end
		end
	end)
end)

“–make the message lowercase”
You really don’t need to when he might want to make his commands have uppercases
image

string.lower makes any string lower case even if all the words are in caps. I just did it to save time instead of writing Cop and cop.

already know
(limit)(limit)(limit)

Better yet, use table.find:

local list = {"cop", "Cop", "Police", "Popo", "911", "Officer", "Cadet", "Sergeant"}
game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        if table.find(list, message) then
            nextbots.Cop:Clone().Parent = workspace
        else
            print("Couldn't find a word within the list tabel.")
        end
    end)
end)

(i didn’t correct the typos)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.