Script only works once

Currently, I can only do !start once. After I do !ref (which refreshes the map), the !start breaks.

	local Players = game:GetService("Players")
	local serverstorage = game:GetService("ServerStorage")
	local replicatedStorage = game:GetService("ReplicatedStorage")

	local AmountOfPlayers = #Players:GetPlayers()
	local folder = workspace.Game.Scriptable.Pillars:GetChildren()
	local amountValue = #folder

	local PlayersService = game:GetService("Players")
	local StringToDetect = "!start"
	local string2 = "!ref"

	Players.PlayerAdded:Connect(function(Player)
AmountOfPlayers = AmountOfPlayers + 1
	end)

	Players.PlayerRemoving:Connect(function(Player)
AmountOfPlayers = AmountOfPlayers - 1
	end)

	PlayersService.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message1)
	print(AmountOfPlayers)
	if string.find(string.lower(Message1), string.lower(string2)) then
		workspace.Game.Scriptable.Pillars:Destroy()
		local pillarClone = replicatedStorage.Pillars:Clone()
		pillarClone.Name = "Pillars"
		pillarClone.Parent = workspace.Game.Scriptable
	end
end)
Player.Chatted:Connect(function(Message)
	print(AmountOfPlayers)
	if string.find(string.lower(Message), string.lower(StringToDetect)) then
		amountValue = #folder
		repeat wait(0.0001)
			folder[math.random(#folder)]:Destroy()
		until #(workspace.Game.Scriptable.Pillars:GetChildren()) == #Players:GetPlayers()
		
		if #(workspace.Game.Scriptable.Pillars:GetChildren()) == #Players:GetPlayers() then
		end
	end
end)
	end)
1 Like

Try adding this



spawn(function()
	warn("Loading...")
	wait(.5)
	
	warn("Loaded!")
	
local Players = game:GetService("Players")
local serverstorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")

local AmountOfPlayers = #Players:GetPlayers()
local folder = workspace.Game.Scriptable.Pillars:GetChildren()
local amountValue = #folder

local PlayersService = game:GetService("Players")
local StringToDetect = "!start"
local string2 = "!ref"

Players.PlayerAdded:Connect(function(Player)
	AmountOfPlayers = AmountOfPlayers + 1
end)

Players.PlayerRemoving:Connect(function(Player)
	AmountOfPlayers = AmountOfPlayers - 1
end)

PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message1)
		print(AmountOfPlayers)
		if string.find(string.lower(Message1), string.lower(string2)) then
			workspace.Game.Scriptable.Pillars:Destroy()
			local pillarClone = replicatedStorage.Pillars:Clone()
			pillarClone.Name = "Pillars"
			pillarClone.Parent = workspace.Game.Scriptable
		end
	end)
	Player.Chatted:Connect(function(Message)
		print(AmountOfPlayers)
		if string.find(string.lower(Message), string.lower(StringToDetect)) then
			amountValue = #folder
			repeat wait(0.0001)
				folder[math.random(#folder)]:Destroy()
			until #(workspace.Game.Scriptable.Pillars:GetChildren()) == #Players:GetPlayers()

			if #(workspace.Game.Scriptable.Pillars:GetChildren()) == #Players:GetPlayers() then
				end
			end
		end)
	end)
end)
```

Currently, the function you added in breaks the script.

1 Like

I think the Repeat wait() is breaking the !start

Ah, alright. Do you know another alternative for the repeat wait?

2 Likes

You can try using while wait() do

1 Like

or try this >

	
local Players = game:GetService("Players")
local serverstorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")

local AmountOfPlayers = #Players:GetPlayers()
local folder = workspace.Game.Scriptable.Pillars:GetChildren()
local amountValue = #folder

local PlayersService = game:GetService("Players")
local StringToDetect = "!start"
local string2 = "!ref"

Players.PlayerAdded:Connect(function(Player)
	AmountOfPlayers = AmountOfPlayers + 1
end)

Players.PlayerRemoving:Connect(function(Player)
	AmountOfPlayers = AmountOfPlayers - 1
end)

PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message1)
		print(AmountOfPlayers)
		if string.find(string.lower(Message1), string.lower(string2)) then
			workspace.Game.Scriptable.Pillars:Destroy()
			local pillarClone = replicatedStorage.Pillars:Clone()
			pillarClone.Name = "Pillars"
			pillarClone.Parent = workspace.Game.Scriptable
		end
	end)
	Player.Chatted:Connect(function(Message)
		print(AmountOfPlayers)
		if string.find(string.lower(Message), string.lower(StringToDetect)) then
			amountValue = #folder
			while wait() do
			
			repeat task.wait()
				folder[math.random(#folder)]:Destroy()
		until	 #(workspace.Game.Scriptable.Pillars:GetChildren()) == #Players:GetPlayers()

			if #(workspace.Game.Scriptable.Pillars:GetChildren()) == #Players:GetPlayers() then
				end
			end
			
			end
		end)
	end)
 

[Never made this btw so it might not work still]

1 Like

Maybe it’s not working because you’re destroying all of the clones?

2 Likes

No, it still has the folder in it. I’m deleting the old folder to add a new once in.

1 Like

Instead of doing repeat wait() you could do a while loop?

while #(workspace.Game.Scriptable.Pillars:GetChildren()) ~= #Players:GetPlayers()

1 Like

Alright, thank you! I will try that.

1 Like
local Players = game:GetService("Players")
local Server = game:GetService("ServerStorage")
local Replicated = game:GetService("ReplicatedStorage")

local Pillar = Replicated:WaitForChild("Pillars")
local GameFolder = workspace:WaitForChild("Game")
local Scriptable = GameFolder:WaitForChild("Scriptable")
local PillarFolder = Scriptable:WaitForChild("Pillars")
local AmountOfPlayers = nil

Players.PlayerAdded:Connect(function(Player)
	AmountOfPlayers = #Players:GetPlayers()
	
	Player.Chatted:Connect(function(Message)
		print(AmountOfPlayers)
		
		if string.find(string.lower(Message), "!ref") then
			PillarFolder:Destroy()
			local pillarClone = Pillar:Clone()
			pillarClone.Name = "Pillars"
			pillarClone.Parent = Scriptable
			
		elseif string.find(string.lower(Message), "!start") then
			local Pillars
			repeat task.wait()
				Pillars = PillarFolder:GetChildren()
				Pillars[math.random(#Pillars)]:Destroy()
			until #Pillars == #Players:GetPlayers()

			if #(PillarFolder:GetChildren()) == #Players:GetPlayers() then
			end
		end
	end)
end)

Players.PlayerRemoving:Connect(function(Player)
	AmountOfPlayers = #Players:GetPlayers()
end)

It works, but it errors after I do !restart and then !start again.

There isn’t a command named “!restart” but the command named “!ref” deletes the Pillars folder which is referenced inside the command named “!start” if this isn’t intended then remove the
PillarFolder:Destroy() line.

I meant ref, sorry about that.

What I wanted to do was basically make the original parts again.

You’d need to loop over the folder’s children and then destroy those instead of the entire folder. Is the instance named “Pillars” inside of ReplicatedStorage a folder of pillars? If so the code which clones that will need to be changed slightly as well.

Mhm, it’s the exact same clone as the ones from workspace.

local Players = game:GetService("Players")
local Server = game:GetService("ServerStorage")
local Replicated = game:GetService("ReplicatedStorage")

local StoragePillars = Replicated:WaitForChild("Pillars")
local GameFolder = workspace:WaitForChild("Game")
local Scriptable = GameFolder:WaitForChild("Scriptable")
local WorkspacePillars = Scriptable:WaitForChild("Pillars")
local AmountOfPlayers = nil

Players.PlayerAdded:Connect(function(Player)
	AmountOfPlayers = #Players:GetPlayers()

	Player.Chatted:Connect(function(Message)
		print(AmountOfPlayers)

		if string.find(string.lower(Message), "!ref") then
			for _, Pillar in ipairs(WorkspacePillars:GetChildren()) do
				Pillar:Destroy()
			end
			
			for _, Pillar in ipairs(StoragePillars:GetChildren()) do
				local PillarClone = Pillar:Clone()
				PillarClone.Name = "Pillars"
				PillarClone.Parent = WorkspacePillars
			end

		elseif string.find(string.lower(Message), "!start") then
			local Pillars
			repeat task.wait()
				Pillars = WorkspacePillars:GetChildren()
				Pillars[math.random(#Pillars)]:Destroy()
			until #Pillars == #Players:GetPlayers()
		end
	end)
end)

Players.PlayerRemoving:Connect(function(Player)
	AmountOfPlayers = #Players:GetPlayers()
end)

Also, do you know how to make a player spawn on their own pillar using this?