So basically this is a script so it spawns in a random build, and if a player types in the build in chat correctly, they get 1 point (the value is called guesses). It's supposed to wait either until someone guesses the build and the build is removed or wait 60 seconds but it does neither, it just goes back through the loop and spawns another build in. Why is this happening?

-- Variables -- 

local rs = game:GetService("ReplicatedStorage")
local serverstorage = game:GetService("ServerStorage")
local builds = serverstorage:WaitForChild("Builds")
local roundlength = 60
local status = rs:WaitForChild("Status")

-- Script --

game.Players.PlayerAdded:Connect(function(plr)
	player = plr
end)

while true do
	status.Value = "Waiting for at least 2 players."
	
	repeat wait() until game.Players.NumPlayers >= 1
	
	status.Value = "Intermission"
	
	wait(2)
	
	local AvailableBuilds = builds:GetChildren()
	
	local ClonedBuild = AvailableBuilds[math.random(1,#AvailableBuilds)]
	
	status.Value = "Build chosen!"
	
	local ClonedBuild = ClonedBuild:Clone()
	ClonedBuild.Parent = workspace

	
	
	repeat wait() until workspace[ClonedBuild.Name] == nil or wait(roundlength)
	print("Guessed")
	
	
	player.Chatted:Connect(function(msg)
		msg = string.lower(msg)
		if msg == ClonedBuild.Name then
			player.leaderstats.Guesses.Value = player.leaderstats.Guesses.Value + 1
			ClonedBuild:Destroy()
			status.Value = player.Name.. " guessed the build correctly. It is ".. ClonedBuild.name.. "!"
			
			end
		end)
	end

EDIT:

-- Variables -- 

local rs = game:GetService("ReplicatedStorage")
local serverstorage = game:GetService("ServerStorage")
local builds = serverstorage:WaitForChild("Builds")
local roundlength = 60
local status = rs:WaitForChild("Status")

-- Script --

while true do
	status.Value = "Waiting for at least 2 players."
	
	repeat wait() until game.Players.NumPlayers >= 1
	
	status.Value = "Intermission"
	
	wait(2)
	
	AvailableBuilds = builds:GetChildren()
	
	ClonedBuild = AvailableBuilds[math.random(1,#AvailableBuilds)]
	
	status.Value = "Build chosen!"
	
	ClonedBuild = ClonedBuild:Clone()
	ClonedBuild.Parent = workspace

	
	
	repeat wait() until workspace[ClonedBuild.Name] == nil or wait(roundlength)
	print("Guessed")
	end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		msg = string.lower(msg)
		if msg == ClonedBuild.Name then
			player.leaderstats.Guesses.Value = player.leaderstats.Guesses.Value + 1
			ClonedBuild:Destroy()
			status.Value = player.Name.. " guessed the build correctly. It is ".. ClonedBuild.name.. "!"
			end
			end)
end)

18 comments

0

You have the Chatted event after the loop, so it wont work since its stuck in the loop.

0

How would I fix this?

0

Try putting it before it, that may work. Otherwise i'd implement a while loop and stick it inside there. I dont use repeat often so i dont know much about it.

0

It is a while loop though.

0

That wouldn't work as if I were to make another while true do loop then it wouldn't be able to get the info from the other while true do loop.

0

Did you try just putting Chatted above the repeat?

0

Then it wouldn't get the data if I put it above it.

0

But again, did you try it? Its able to access the local variables you set as paramaters.

0

I tried it, didn't work. It wouldn't get the data when I tried it as it needs some of the data from the while loop.

0

I also tried to rearrange it in a different way, I'll edit the post and add it at the bottom.

0

Interesting. I do see another glaring issue though, if it goes for 60 seconds and no one guesses it you need to destroy it.

0

Okay.

0

I added after the repeat wait 'if workspace[ClonedBuild.Name] then workspace[ClonedBuild.Name]:Destroy()'

0

Put the Chatted event inside the loop.

0

Steam, it is in the first version.

0

I know it cant possibly work if its after. Thats outside of the scope.

0

Do you know how I could fix this/

0

?