I try to make tool durability that I add a number value inside the tool and once the toolbecome 0 the tool destroy and I want to find a way to make a bar too show your tool durability how should I make that?
With a ScreenGui which displays whenever the tool is equipped.
Use :GetPropertyChangedSignal()
local NumberValue -- path
NumberValue:GetPropertyChangedSignal("Value"):Connect(function()
if NumberValue.Value == 0 then
script.Parent:Destroy()
end
end)
1 Like
for the ui, same thing. you just change text.
1 Like
local Players = game:GetService("Players")
local Tool = script.Parent
--show gui when tool is equipped
Tool.Equipped:Connect(function()
local Character = Tool.Parent
local Player = Players:GetPlayerFromCharacter(Character)
local PlayerGui = Player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui --change screengui to name of gui in startergui
if not ScreenGui.Enabled then
ScreenGui.Enabled = true
end
end)
--hide gui when tool is unequipped
Tool.Unequipped:Connect(function()
local Player = Tool:FindFirstAncestorWhichIsA("Player")
local PlayerGui = Player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui --change screengui to name of gui in startergui
if ScreenGui.Enabled then
ScreenGui.Enabled = false
end
end)
local Players = game:GetService("Players")
local Tool = script.Parent
local Durability = Tool:WaitForChild("Durability")
--show gui when tool is equipped
Tool.Equipped:Connect(function()
local Character = Tool.Parent
local Player = Players:GetPlayerFromCharacter(Character)
local PlayerGui = Player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui --change screengui to name of gui in startergui
if not ScreenGui.Enabled then
ScreenGui.Enabled = true
end
end)
--hide gui when tool is unequipped
Tool.Unequipped:Connect(function()
local Player = Tool:FindFirstAncestorWhichIsA("Player")
local PlayerGui = Player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui --change screengui to name of gui in startergui
if ScreenGui.Enabled then
ScreenGui.Enabled = false
end
end)
--reduce durability when tool is used
Tool.Activated:Connect(function()
Durability.Value -= 1
end)
--when durability is decreased to 0 destroy the tool
--whenever durability is changed update the gui
Durability.Changed:Connect(function(NewValue)
if Tool.Parent:IsA("Model") then
local Player = Players:GetPlayerFromCharacter(Tool.Parent)
local PlayerGui = Player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui
local TextLabel = ScreenGui.TextLabel
TextLabel.Text = NewValue
elseif Tool.Parent:IsA("Backpack") then
local Player = Tool:FindFirstAncestorWhichIsA("Player")
local PlayerGui = Player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui
local TextLabel = ScreenGui.TextLabel
TextLabel.Text = NewValue
end
if NewValue <= 0 then
Tool:Destroy()
end
end)
1 Like