RunService not working?

Hi, I’m working on a cannon/turret, and I’m using run-service to make the turret move seamlessly/constantly, I made a script that when the player sits in a seat the player takes control of the cannon, but the part of the script that stops the run service when the player leaves the seat, doesnt seem to be working…or I’m doing something wrong…

Here’s my code:

local runService = game:GetService("RunService")

    seat.DescendantAdded:Connect(function()
    	if seat.Occupant then
    		runService.RenderStepped:Connect(function()
    			Remote:FireServer(Mouse.Hit.Position)
    		end)
    	end
    end)

    seat.DescendantRemoving:Connect(function()
    	if seat.Occupant == false then
    		runService:Disconnect()
    	end
    end)

Any help is much appreciated! :grinning: :+1:

You need to store the event in a variable and disconnect that

local event

seat.DescendantAdded:Connect(function()
	if seat.Occupant then
		event = runService.RenderStepped:Connect(function()
			Remote:FireServer(Mouse.Hit.Position)
		end)
	end
end)

seat.DescendantRemoving:Connect(function()
	if seat.Occupant == false then
		if event then event:Disconnect() end
	end
end)

Also, instead of DescendantAdded and DescendantRemoving, can’t you use GetPropertyChangedSignal on the Occupant property?

I’m already doing what you said…

And its still not working…

I do not mean that, look at my code that I’ve provided, you need to put the event you want to disconnect into a variable and disconnect the event in that variable. You’re trying to disconnect the RunService service, not the event

Just use :BindToRenderStep()
All of this :Connect( :Disconnect( jazz is super messy and prone to error

function seatFunction()
    Remote:FireServer(Mouse.Hit.Position)
end

--How to bind
rs:BindToRenderStep("SeatEvent", 1, seatFunction)

--How to unbind
rs:UnbindFromRenderStep("SeatEvent")

Could you explain more what you mean?

I’m not sure what you mean, I did this:

local runService = game:GetService("RunService")

 --descendant added code here...

seat.DescendantRemoving:Connect(function()
	if seat.Occupant == false then
		if runService then runService:Disconnect() end
	end
end)

I mean this

local runService = game:GetService("RunService")

local event

seat.DescendantAdded:Connect(function()
	if seat.Occupant then
		event = runService.RenderStepped:Connect(function()
			Remote:FireServer(Mouse.Hit.Position)
		end)
	end
end)

seat.DescendantRemoving:Connect(function(r)
	if seat.Occupant == false then
		if event then event:Disconnect() end
	end
end)

Theres not too much more to explain, its relatively straightforward

When you want to bind your function to renderstep you run BindToRenderStep with name “SeatEvent” and priority 1 (but neither of these really matter), then give it your function
When you want to unbind your function from renderstep you run UnbindFromRenderStep with the given name

1 Like

What is the event for? what information is that variable supposed to hold?

That variable holds the RenderStepped event you made, it’s how you’re able to disconnect the event whe nyou don’t want it to run anymore

If someone is sitting, make a RenderStepped event to fire a RemoteEvent and store it in event
If no one is sitting anymore, check if the event variable contains something, if it does, then we disconnect whatever is stored in it since we know it’s always going to contain an Event

I tried your code, and it still doesnt disconnect the RenderStepped…

Could it maybe be how the code is set up? Maybe try my previous suggestion about GetPropertyChangedSignal?

local runService = game:GetService("RunService")

local event

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant then
		event = runService.RenderStepped:Connect(function()
			Remote:FireServer(Mouse.Hit.Position)
		end)
	else
		event:Disconnect()
	end
end)

So it ensures only 1 event is made?

1 Like

Yeah, that works!

Thanks for the help, I really appreciate it! :grinning: :+1:

1 Like

If needed as well, this should also work, using BindToRenderStep as @PapaBreadd mentioned

local runService = game:GetService("RunService")

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant then
		runService:BindToRenderStep("SeatEvent", 1, function()
			Remote:FireServer(Mouse.Hit.Position)
		end)
	else
		runService:UnbindFromRenderStep("SeatEvent")
	end
end)

Hopefully I did the function right, haven’t tested it but it should hopefully work. If not, then you can go back to using the disconnection method

2 Likes

But it seems I have a new problem, For some reason while testing it i noticed that the turret follows all players mouse’s… I’m not sure how to fix this.

Link to the game:

Oh, I believe the issue is because there’s no verification on who it has to follow

Maybe try this?

local runService = game:GetService("RunService")
local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer

local event

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local humanoid = seat.Occupant
	if humanoid then
		local player = Players:GetPlayerFromCharacter(humanoid.Parent)
		if not player or player ~= localPlayer then
			return
		end
		event = runService.RenderStepped:Connect(function()
			Remote:FireServer(Mouse.Hit.Position)
		end)
	else
		if event then
			event:Disconnect() 
		end
	end
end)
1 Like

I don’t know why, but that breaks the whole script… I added that code in and now the run-service doesnt disconnect…

EDIT: I did something wrong, It works now! :grinning: :+1: Thanks for the help!

1 Like