Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to pause run service when a value becomes false?

local script: when the left arrow key is pressed a bool value is set to true and is set back to false when it is released

-- The local script--
local uis = game:GetService("UserInputService")
local sm = game.Workspace.valfolder.shootmain --bool val
uis.InputBegan:Connect(function(input)
		if input.KeyCode==Enum.KeyCode.Left then
		print("Then left pressed")
		sm.Value = true
			game.ReplicatedStorage.fireshot:FireServer()
	end
end)

uis.InputEnded:Connect(function(input)
	if input.KeyCode==Enum.KeyCode.Left then
		sm.Value = false
			print("left arrow remote event start")
	end
end)

server script: when the value becomes true, the RunService begins but the issue occurs when the value is set to false it does not stop.

local sm = game.Workspace.valfolder.shootmain --bool val
local runService = game:GetService('RunService')
local Stepped 
game.ReplicatedStorage.fireshot.OnServerEvent:Connect(function(player)
Stepped = runService.Stepped:Connect(function()
	if sm.Value == false then
		print('Printing')
if sm.Value == true then
wait(.5)
Stepped:Disconnect()
			end
		end
	end)
end)

ps. how can I add a wait in the RunService so the print has a wait?

1 answer

✓ Accepted answer

(I deleted the last answer)

DevForum: How to Terminate RunService - Render Loop by beartikel

I tried to recreate this and this is what I came up with:

local sm = game.Workspace.valfolder.shootmain --bool val
local runService = game:GetService('RunService')
local Stepped 

game.ReplicatedStorage.fireshot.OnServerEvent:Connect(function(player)
	Stepped = runService.Stepped:Connect(function()
		if sm.Value == false then
			print('Printing')
		end
	end)

	sm:GetPropertyChangedSignal("Value"):Connect(function()
		if sm.Value == true then
			wait(.5)
			Stepped:Disconnect()
		end
	end)
end)

If you want to wait, the only way I know without disrupting other codes would be to use coroutine.

local sm = game.Workspace.valfolder.shootmain --bool val

game.ReplicatedStorage.fireshot.OnServerEvent:Connect(function(player)
	coroutine.resume(coroutine.create(function()
		while true do
			if sm.Value == true then return end
			wait(1)
			print('Printing')
		end
	end))
end)

1 comment

0

thx

This discussion is preserved as it was. New answers, comments, and votes are closed.