Simple event not working

Goal: When the player steps on the part, their second countshows in the output.
Issue: Nothing shows up in the output bar, I’m not that great with events so I probably messed something up.

By the way, counts is an int value! I put it in ServerStorage.

--  variables
local counts = game.ServerStorage.Counts
local part = game.Workspace.IntValueTest

-- counter
while true do
	counts.Value = counts.Value + 1
	wait(1)
end

-- event
part.Touched:Connect(function(hit)
	print(counts.Value)
	wait(1)
end)

thank you!

Try this instead.

--  variables
local counts = game.ServerStorage.Counts
local part = game.Workspace.IntValueTest

-- counter
coroutine.wrap(function()
while true do
	counts.Value = counts.Value + 1
	wait(1)
end

end)()

-- event
part.Touched:Connect(function(hit)
	print(counts.Value)
	wait(1)
end)
1 Like

Thank you so much, it worked! May I ask, what does the coroutine.wrap(function() do? :/D

I know that the coroutine.wrap(function() runs functions in the background. But, if you want to know more then look at this link, coroutine.

1 Like
local counts = game.ServerStorage.Counts
local part = game.Workspace.IntValueTest

part.Touched:Connect(function(hit)
	print(counts.Value)
	wait(1)
end)

while true do
	counts.Value = counts.Value + 1
	wait(1)
end
1 Like

It works. But, coroutine.wrap ensures that you can continue under that function.

Yes but you can avoid it by simply moving the ‘forever’ loop below the event connection.