I have a weapon tool with durability and everything works as intended, but for some reason when the tool breaks and I grab another one it stops working. As in it appears the scripts inside the tool stop working and it can no longer be used. Just to note, I’m having no problems with the GUI, just the scripting. (Also the reason for the incredibly low durability is just so that I could test it faster.)
Server script in tool:
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Functions = ServerScriptService:WaitForChild("Functions")
local Modules = ServerScriptService:WaitForChild("Modules")
local CModules = ReplicatedStorage:WaitForChild("Modules")
local RaycastHitbox = require(CModules:WaitForChild("RaycastHitboxV4"))
local Utility = require(CModules:WaitForChild("Utility"))
local GlobalCooldown = require(Modules:WaitForChild("GlobalCooldown"))
local Engine = require(Modules:WaitForChild("Engine"))
local Tool = script.Parent
local Handle = Tool:WaitForChild("Weapon")
local Remote = Tool:WaitForChild("Remote")
local Animation = Tool:WaitForChild("Animation")
local Weapon = Tool.Weapon
local DURABILITY = Instance.new("IntValue", Weapon)
DURABILITY.Name = "Durability"
DURABILITY.Value = 5
local isEquipped
local function getOwner(): Player?
if isEquipped then
return Players:GetPlayerFromCharacter(Tool.Parent)
else
local P = Tool.Parent ~= nil and Tool.Parent.Parent or nil
return P and P:IsA("Player") and P or nil
end
end
Tool.Equipped:Connect(function()
isEquipped = true
end)
Tool.Unequipped:Connect(function()
isEquipped = false
end)
local RNG = Random.new()
local SwingSound = Handle:WaitForChild("Swing")
local BreakSound = Handle:WaitForChild("Break")
local HitSound = script:WaitForChild("Hit")
local AttackAnimation = Animation:WaitForChild("Attack")
Tool.Equipped:Connect(function()
local Character = Tool.Parent
local Torso = Character:FindFirstChild("Torso")
local motor = Torso:FindFirstChild(Tool.Name) or Instance.new("Motor6D")
motor.Part0 = Torso
motor.Part1 = Handle
motor.Name = Tool.Name
motor.Parent = Torso
end)
local MIN_DAMAGE, MAX_DAMAGE = 15, 20
local hitList: { Player } = {}
local canHit
Remote.RequestAttack.OnServerInvoke = function(player: Player)
if player ~= getOwner() or GlobalCooldown.IsOnCooldown(player) then return false end
local character = player.Character
local animationLength = Utility.GetAnimationLength(AttackAnimation)
SwingSound:Play()
GlobalCooldown.AddCooldown(player, animationLength + 2.6)
canHit = true
task.delay(animationLength, function()
hitList = {}
canHit = nil
end)
return true
end
Remote.OnHit.OnServerEvent:Connect(function(player, part: Instance, position: Vector3)
if not canHit or player ~= getOwner() then return end
local targetPlayer = Players:GetPlayerFromCharacter(part.Parent)
local targetCharacter = targetPlayer and targetPlayer.Character or nil
if not targetPlayer or table.find(hitList, targetPlayer) or not targetCharacter or targetCharacter:FindFirstChildOfClass("ForceField") then return end
if not Engine:CanInteract(player, targetPlayer) then return end
local character = player.Character
local targetHumanoid = targetCharacter:FindFirstChildOfClass("Humanoid")
if not character or not targetHumanoid then return end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
local targetHumanoidRootPart = targetCharacter:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart or not targetHumanoidRootPart then return end
local distance = math.abs((humanoidRootPart.Position - targetHumanoidRootPart.Position).Magnitude)
if distance > 10 then return end
local raycastParams = RaycastParams.new()
raycastParams.IgnoreWater = true
raycastParams.FilterDescendantsInstances = Utility.GetAllPlayerCharacter()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local castResult = workspace:Raycast(humanoidRootPart.Position, CFrame.lookAt(humanoidRootPart.Position, targetHumanoidRootPart.Position).LookVector * Vector3.new(distance, distance, distance), raycastParams)
if castResult then return end
table.insert(hitList, targetPlayer)
local damage = RNG:NextInteger(MIN_DAMAGE, MAX_DAMAGE)
targetHumanoid:TakeDamage(damage)
task.spawn(function()
local EffectPart = Instance.new("Part")
EffectPart.Anchored = true
EffectPart.Position = position
EffectPart.CanQuery = false
EffectPart.CanTouch = false
EffectPart.CanCollide = false
EffectPart.Shape = Enum.PartType.Ball
EffectPart.Size = Vector3.new(1, 1, 1)
EffectPart.Color = Color3.fromRGB(255, 255, 255)
EffectPart.Material = Enum.Material.Neon
EffectPart.Transparency = .5
EffectPart.Parent = workspace
Functions:WaitForChild("ClientTween"):Invoke(EffectPart, {.75,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut}, {Size = Vector3.new(5,5,5),Transparency= 1}, true)
EffectPart:Destroy()
end)
task.spawn(function()
local soundPart = Instance.new("Part")
soundPart.Anchored = true
soundPart.Position = position
soundPart.CanQuery = false
soundPart.CanTouch = false
soundPart.CanCollide = false
soundPart.Transparency = 1
soundPart.Parent = workspace
local hitSound: Sound = HitSound:Clone()
hitSound.Parent = soundPart
hitSound:Play()
hitSound.Ended:Wait()
soundPart:Destroy()
end)
DURABILITY.Value -= 1
if 0 >= DURABILITY.Value then
player.PlayerGui.Cooldown:Destroy()
player.PlayerGui.Durability:Destroy()
BreakSound.PlayOnRemove = true
character:WaitForChild("Humanoid"):UnequipTools()
task.wait()
Tool:Destroy()
end
end)
Local script in tool:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local TS = game:GetService("TweenService")
local CModules = ReplicatedStorage:WaitForChild('Modules')
local RaycastHitbox = require(CModules:WaitForChild("RaycastHitboxV4"))
local Tool = script.Parent
local Handle = Tool:WaitForChild("Weapon")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Weapon = script.Parent.Weapon
local Durability = Weapon:WaitForChild("Durability")
local MaxDurability = 5
local GUI = script:WaitForChild("Cooldown")
local DGUI = script:WaitForChild("Durability")
local Animation = Tool:WaitForChild("Animation")
local Remote = Tool:WaitForChild("Remote")
local I = Character:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(Animation:WaitForChild("Idle")) :: AnimationTrack
local A = Character:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(Animation:WaitForChild("Attack")) :: AnimationTrack
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = { Character }
Params.FilterType = Enum.RaycastFilterType.Blacklist
local Hitbox = RaycastHitbox.new(Handle)
Hitbox.RaycastParams = Params
Tool.Equipped:Connect(function()
GUI.Parent = Player.PlayerGui
DGUI.Parent = Player.PlayerGui
I:Play()
end)
Tool.Unequipped:Connect(function()
GUI.Enabled = false
GUI.MainBar.Bar.Size = UDim2.new(1,0,1,0)
GUI.Parent = script
DGUI.Parent = script
A:Stop()
I:Stop()
end)
Tool.Activated:Connect(function()
local canHit = Remote.RequestAttack:InvokeServer()
if not canHit then return end
GUI.Enabled = true
GUI.MainBar.Bar.Size = UDim2.new(1,0,1,0)
local Tween = TS:Create(GUI.MainBar.Bar,TweenInfo.new(3.6,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut) , {Size = UDim2.fromScale(0,1)})
Tween:Play()
Hitbox:HitStart()
A:Play()
A.Stopped:Wait()
Hitbox:HitStop()
Tween.Completed:Wait()
GUI.Enabled = false
end)
Hitbox.OnHit:Connect(function(_, humanoid, result)
Remote.OnHit:FireServer(humanoid, result.Position)
end)
local function UpdateDurability()
local CurrentValue = Durability.Value
local Formula = math.clamp(CurrentValue/MaxDurability, 0, 1)
DGUI.MainBar.Bar:TweenSize(UDim2.new(Formula, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.15, false)
end
UpdateDurability()
Durability.Changed:Connect(function()
UpdateDurability()
end)