So I’ve been making an RPG system and I was working on a staff that heals nearby players. Somehow, I managed to mess it up and it does not work!!! Please help.
My Tool Script:
local tool = script.Parent
local nextActivate = os.clock()
local debounceTime = script.Parent.Cooldown.Value -- Change this for a different tool cooldown length
local attacking = false
local canDMG = true
local cooldowns = {}
tool.HealingOrb.Touched:Connect(function(hit)
if canDMG and attacking then
canDMG = false
local otherPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
if otherPlayer then
if cooldowns[otherPlayer.UserId] then
return
end
cooldowns[otherPlayer.UserId] = true
task.delay(0.5, function()
cooldowns[otherPlayer.UserId] = nil
end)
if otherPlayer.Character.Humanoid.Health > 0 then
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
local damageFormula = (script.Parent.HealAmount.Value + (script.Parent.HealIncrease.Value * plr.leaderstats.Level.Value))
if math.random(1, 25) == 1 then
damageFormula = damageFormula * 2
end
hit.Parent.Humanoid.Health += damageFormula
end
end
task.wait(debounceTime)
canDMG = true
end
end)
tool.Activated:Connect(function()
if os.clock() > nextActivate then
nextActivate = os.clock() + debounceTime
attacking = true
tool.HealingOrb.Transparency = 0.75
task.wait(debounceTime)
tool.HealingOrb.Transparency = 1
attacking = false
end
end)
I don’t really see any problem from the code it self. From what I understand from the code is that, it will only heal the player once, depending on the debounce time and only the player that was hit by the part and only to the first player.
I am still not sure what really is the problem here could you provide more information about what do you really want to achieve here.
I see then you need to change the structure of the code. Because the current, like what I said earlier only heals once.
I could help you make the script but I’m too lazy to make one because there are a lot of questions that needs to be answered, such as: Is the healing continuous when the tool is activated? Does it have a certain healing per second? What is the size of the aura?
Honestly, I am not sure if this is even gonna work. I haven’t tried using it since I don’t have the model to the tool. Try using it see if there are any problems and I would gladly fix it.
local Players = game:GetService("Players")
local tool = script.Parent :: Tool
local healingOrb = tool.HealingOrb :: Part
local localPlayer = Players:GetPlayerFromCharacter(tool.Parent)
local healingActive = false
local orbSize = healingOrb.Size
local nextActivate = os.clock()
local debounceTime = tool.Cooldown.Value :: number
local level = localPlayer.leaderstats.Level :: NumberValue
local healAmount = tool.HealAmount :: NumberValue
local healIncrease = tool.HealIncrease :: NumberValue
local function HealClosePlayers()
local playerList = Players:GetPlayers()
for _, player in playerList do
local character = player.Character
if not character or player == localPlayer then
continue
end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart") :: Part
if humanoidRootPart and (healingOrb.Position - humanoidRootPart.Position).Magnitude <= orbSize then
local humanoid = character:FindFirstChild("Humanoid") :: Humanoid
if humanoid then
local damageFormula = healAmount.Value + healIncrease.Value * level.Value
if math.random(1, 25) == 1 then
damageFormula = damageFormula * 2
end
humanoid.Health += damageFormula
end
end
end
end
local function ActivateHealing()
while healingActive do
HealClosePlayers()
task.wait(0.5)
end
end
tool.Activated:Connect(function()
if os.clock() >= nextActivate then
nextActivate = os.clock() + debounceTime
healingActive = true
tool.HealingOrb.Transparency = 0.75
task.spawn(ActivateHealing)
task.wait(debounceTime)
tool.HealingOrb.Transparency = 1
healingActive = false
end
end)
Error:
Players.haydengamingdo.Backpack.Arcadian Staff.MainScript:34: attempt to compare number <= Vector3
Stack Begin
Script ‘Players.haydengamingdo.Backpack.Arcadian Staff.MainScript’, Line 34 - function HealClosePlayers
Script ‘Players.haydengamingdo.Backpack.Arcadian Staff.MainScript’, Line 55 - function ActivateHealing
Stack End
Script (Changed a bit!) :
local Players = game:GetService("Players")
local tool = script.Parent
repeat wait() until Players:GetPlayerFromCharacter(tool.Parent)
local localPlayer = Players:GetPlayerFromCharacter(tool.Parent)
local healingActive = false
local healingOrb = script.Parent:WaitForChild("HealingOrb")
local orbSize = healingOrb.Size
local nextActivate = os.clock()
local debounceTime = tool.Cooldown.Value :: number
local level = localPlayer.leaderstats.Level :: NumberValue
local healAmount = tool.HealAmount :: NumberValue
local healIncrease = tool.HealIncrease :: NumberValue
local function HealClosePlayers()
local playerList = Players:GetPlayers()
for _, player in playerList do
local character = player.Character
if not character or player == localPlayer then
continue
end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart") :: Part
if humanoidRootPart then
if (healingOrb.Position - humanoidRootPart.Position).Magnitude <= orbSize then
local humanoid = character:FindFirstChild("Humanoid") :: Humanoid
if humanoid then
local damageFormula = healAmount.Value + (healIncrease.Value * level.Value)
if math.random(1, 25) == 1 then
damageFormula = damageFormula * 2
end
humanoid.Health += damageFormula
end
end
end
end
end
local function ActivateHealing()
while healingActive do
HealClosePlayers()
task.wait(0.5)
end
end
tool.Activated:Connect(function()
if os.clock() >= nextActivate then
nextActivate = os.clock() + debounceTime
healingActive = true
tool.HealingOrb.Transparency = 0.75
task.spawn(ActivateHealing)
task.wait(debounceTime)
tool.HealingOrb.Transparency = 1
healingActive = false
end
end)