What's wrong with this script? (no errors, just won't run)

The time marker is not going up after leaving the safezone and I assume the coroutine isn’t running because the print isn’t going. Can anyone help?

local safeCheck = coroutine.wrap(function()
	while task.wait(1) do
		game.Workspace.Safezone.Touched:Connect(function(hmn)
			return true
		end)
		game.Workspace.Safezone.TouchEnded:Connect(function(hmn)
			print("Hi")
			return false
			
		end)
	end
end)

local timeMaker = coroutine.wrap(function()
	if safeCheck() == false then
		while task.wait(1) do
			time.Value += 1
		end
	end
end)
timeMaker()
safeCheck()

You don’t have to constantly connect those events every few seconds. For one, you’re creating more unnecessary memory leaks and two it might be the reason why your code fails.

The issue with that in my code is that the other function is dependent on the loop being a function

Why are you constantly connecting the events in a loop? You should only connect them once. You can use a BoolValue and change its value whenever one of the events is fired, and then you code the conditional statement depending on the BoolValue.

1 Like

Alright I did that and now the print function is working but the time alive is not going up

Please provide your new code here?

local timeMaker = coroutine.wrap(function()
	if safe == false then
		while task.wait(1) do
			time.Value = time.Value + 1
		end
	end
end)

timeMaker()

while task.wait(1) do
	game.Workspace.Safezone.Touched:Connect(function(hmn)
		safe = true
	end)
	game.Workspace.Safezone.TouchEnded:Connect(function(hmn)
		print("Hi")
		safe = false
		
	end)
end

You didn’t even do what I’ve said. You’re still relying on the while loop connecting the events and not using a BoolValue instance.

Then I’m confused since I made a boolvalue (safe in this case)

Please clarify better next time. I was confused.

A few problems left in your code are one, you’re still connecting events constantly every second. Two,

You should just do the conditional statement inside the while loop, it would be much easier.

1 Like

thank you!! there’s one more issue

this line

time.Value = time.Value + 1

in this section

while task.wait(1) do
	if safe == false then
		time.Value = time.Value + 1
	end
	game.Workspace.Safezone.Touched:Connect(function(hmn)
		safe = true
	end)
	game.Workspace.Safezone.TouchEnded:Connect(function(hmn)
		print("Hi")
		safe = false
		
	end)
end

The line gets me the error “attempt to index function with ‘Value’” and i do not know how to fix it. Can you help once again

What is with the time instance? What does it contain? Also you’re not even listening to me at this point, you’re still connecting the events constantly.

The time is a leaderstat which counts how many seconds you have been outside of the safezone and i don’t know how to disconnect them

Is the time instance inside a leaderstats folder?

yes it is
thisisfor30characters

It seems like the time term is a global variable. In other words, use another name for it because the word time just so happens to collide with a global function.

1 Like