I’ve got a script which subtracts the “Charge” IntValue from a tool. I want it to wait atleast 4 seconds after the remoteEvent is fired and then add + 5 every second to the tools “Charge”.
How could I do this?
local ServerScriptService = game:GetService('ServerScriptService')
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local tool = script.Parent
local RemoteEvent = tool.FireFire
local spell = tool.Spell
local Charge = tool.Charge
--Charge is a int value
local toolDummy = game.ReplicatedStorage.EffectsFolder.FireballTrail:Clone()
local Explosion = game.ReplicatedStorage.EffectsFolder.FireballExplosion:Clone()
-- Avatar Fire Cast = 110825053619860
tool.Equipped:Connect(function()
local char = tool.Parent
print("Character equipped:", char)
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
local animator = humanoid:FindFirstChild("Animator")
if animator then
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://110825053619860"
local animTrack = animator:LoadAnimation(animation)
animTrack:Play()
tool.Unequipped:Connect(function()
animTrack:Stop()
end)
end
end
end)
RemoteEvent.OnServerEvent:Connect(function(plr, SetPosition, HitPosition)
local Sub = tool.Charge.Value - 20
local projectile = require(ServerScriptService.Modules.ProjectileMod).new(0.05, workspace:GetDescendants(), Vector3.new(0,0,0), 3, true, toolDummy, Explosion)
local Tween = TweenService:Create(tool.Charge, TweenInfo.new(0.8), {Value = Sub})
Tween:Play()
projectile:Cast(SetPosition, HitPosition, 6, plr)
end)
I looked into it and instead used RunService, I’ll leave the full script here with the changes
local ServerScriptService = game:GetService('ServerScriptService')
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local tool = script.Parent
local RemoteEvent = tool.FireFire
local spell = tool.Spell
local Charge = tool.Charge
--Charge is a intValue
local toolDummy = game.ReplicatedStorage.EffectsFolder.FireballTrail:Clone()
local Explosion = game.ReplicatedStorage.EffectsFolder.FireballExplosion:Clone()
-- Avatar Fire Cast = 110825053619860
local lastFireTime = 0
local rechargeRate = 20 -- Change this value as per your recharge rate
local rechargeInterval = 4 -- 4 seconds
local activeTween = nil -- Store the active Tween
local function rechargeCharge()
while true do
RunService.Heartbeat:Wait()
if tick() - lastFireTime >= rechargeInterval then
local newCharge = math.min(tool.Charge.Value + rechargeRate, 100) -- Ensure Charge does not exceed 100
if activeTween then
activeTween:Cancel() -- Cancel the active Tween if it exists
end
activeTween = TweenService:Create(tool.Charge, TweenInfo.new(12), {Value = newCharge})
activeTween:Play()
break -- Only recharge once after 4 seconds of inactivity
end
end
end
tool.Equipped:Connect(function()
local char = tool.Parent
print("Character equipped:", char)
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
local animator = humanoid:FindFirstChild("Animator")
if animator then
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://110825053619860"
local animTrack = animator:LoadAnimation(animation)
animTrack:Play()
tool.Unequipped:Connect(function()
animTrack:Stop()
end)
end
end
end)
RemoteEvent.OnServerEvent:Connect(function(plr, SetPosition, HitPosition)
if activeTween then
activeTween:Cancel() -- Cancel the active Tween if it exists
end
local Sub = tool.Charge.Value - 20
local projectile = require(ServerScriptService.Modules.ProjectileMod).new(0.05, workspace:GetDescendants(), Vector3.new(0,0,0), 3, true, toolDummy, Explosion)
activeTween = TweenService:Create(tool.Charge, TweenInfo.new(0.8), {Value = Sub})
activeTween:Play()
projectile:Cast(SetPosition, HitPosition, 6, plr)
lastFireTime = tick()
rechargeCharge()
end)
I used a function with RunService.Heartbeat that checked if the time elapsed from when the remoteEvent was fired was greater than the Recharge Interval (4 seconds) and then went form there