Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to pause run service when a value becomes false?

Asked by 2 years ago
Edited 2 years ago

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

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

(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)
0
thx SilverishReign 75 — 2y
Ad

Answer this question