How can I connect a render step/heartbeat function, only when equipped?

How can I connect a render step function, only when equipped?

I have this renderstepped function on a server script that acts as a stun counter. However, as it’s on a serverscript, it has the issue of running even when I do not have it equipped on me. This causes me to have tons of errors fill my screen (As valueToChange and such are only defined after a player equips the tool) and gives me the thought that it would be terribly unoptimized to have it constantly running when nobody is even using the weapon. While statments are also out of question for that reason…

As such, while just adding “If equipped == false then return end” to the function would work for my purposes, it does not fufill my ideas or requirement of the renderstep not firing in the first place.

local function runConnection(_, deltaTime: number)
	if (os.clock() - lastUpdate) < 0.1 then
		return
	end
	if valueToChange.Value <= 0 and attackStun.Value <= 0 then return end
	if valueToChange.Value > 0 then
		valueToChange.Value = math.clamp(valueToChange.Value - deltaTime, 0, math.huge)
	end
	if attackStun.Value > 0 then
		attackStun.Value = math.clamp(attackStun.Value - deltaTime, 0, math.huge)
	end
	print(valueToChange.Value, attackStun.Value)
	lastUpdate = os.clock()
end

RunService.Stepped:Connect(runConnection)
1 Like

You can connect the renderStep when tool is equipped and disconnect when unequipping.

local connection = nil --This is an empty connection variable to store the render connection
local function runConnection(_, deltaTime: number)
	if (os.clock() - lastUpdate) < 0.1 then
		return
	end
	if valueToChange.Value <= 0 and attackStun.Value <= 0 then return end
	if valueToChange.Value > 0 then
		valueToChange.Value = math.clamp(valueToChange.Value - deltaTime, 0, math.huge)
	end
	if attackStun.Value > 0 then
		attackStun.Value = math.clamp(attackStun.Value - deltaTime, 0, math.huge)
	end
	print(valueToChange.Value, attackStun.Value)
	lastUpdate = os.clock()
end

-- For example:
tool.Equipped:Connect(function()
    connection =  RunService.Stepped:Connect(runConnection) --Store the connection
end)

tool.Unequipped:Connect(function()
     connection:Disconnect() --Disconnect the connection
end)
1 Like

This seems to not work for me. I have used a similar method as the aforementioned before after it being recommended to me by the roblox assistant. Oddly enough, the print statements I have added to the scripts have not being firing.
I will say that i have switched the runConnection to a heartbeat() system instead of a Stepped() system, and that the connection is stoned in a connection by itself. Here is the updated code, with more info.:

local lastUpdate = 0

local function runConnection(_, deltaTime: number)
	if (os.clock() - lastUpdate) < 0.1 then
		print("something") --No print.
		return
	end
	if valueToChange.Value <= 0 and attackStun.Value <= 0 then return end
	if valueToChange.Value > 0 then
		valueToChange.Value = math.clamp(valueToChange.Value - deltaTime, 0, math.huge)
	end
	if attackStun.Value > 0 then
		attackStun.Value = math.clamp(attackStun.Value - deltaTime, 0, math.huge)
	end
	print(valueToChange, attackStun) --No print.
	lastUpdate = os.clock()
end

--Everything in this function works as intended, with the connection seemingly not working.

Tool.Equipped:Connect(function()
	equipped = true
	local Character = Tool.Parent
	local Humanoid = Character.Humanoid
	Animations = {
		["BlockingAnim"] = Character:FindFirstChild("Humanoid"):LoadAnimation(Tool.Animation);
		["M1Anim"] = Character:FindFirstChild("Humanoid"):LoadAnimation(Tool.M1);
		["M1AnimNo2"] = Character:FindFirstChild("Humanoid"):LoadAnimation(Tool.M1No2);
		["HeavyAnim"] = Character:FindFirstChild("Humanoid"):LoadAnimation(Tool.HeavyAnim);
		["Parryanim"] = Character:FindFirstChild("Humanoid"):LoadAnimation(Tool.Parryanim);
		["ParEdanime"] = Character:FindFirstChild("Humanoid"):LoadAnimation(Tool.GotParried);
	}
	--ParentalPlayer = Players:GetPlayerFromCharacter(Tool.Parent)
	blocking = Character:WaitForChild("Blocking?")
	ducking = Character:WaitForChild("Ducking?")
	attackStun = Character:WaitForChild("AttackStun?")
	valueToChange = Character:WaitForChild("BlockingStun?")
        --here is the RunConnection
	connection = RunService.Heartbeat:Connect(runConnection)
	valueToChange.Value = 0.1
	attackStun.Value = 0.1
	Animations.M1Anim:Play()
end)

Tool.Unequipped:Connect(function()
	equipped = false
	valueToChange.Value = -1
	attackStun.Value = -1
	connection:Disconnect()
end)

After further testing, the code seems to set valueToChange and attackStun to 0 upon equipping, and I have no idea why. This seems to work differently than when I use to have it in a local script…

Turns out, I’m a buffoon who didn’t understand what INTvalue meant. My bad brother, my bad.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.