Frame not changing in size?

  1. What do you want to achieve?

I am making a durability system for my tool. However, the durability bar itself does not seem to be updating.

  1. What is the issue?

Along with durability, there is a bar (Frame) that should inform you on how much durability a tool has left. This does not seem to be updating.

  1. What solutions have you tried so far?

I tried changing the script up, and tried multiple different methods to resize the Frame, but soon realized that all modifications of the bar, including Color3 changes, did not manipulate the bar at all.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!


(As you can tell, the green bar is not changing)

(Durability changes located in other scripts in the tool, that itself works)

task.wait(0.5)

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Gui = script.Parent
local MainFrame = Gui.MainFrame
local DurabilityBar = MainFrame.DurabilityBar
local Tool = Gui.Parent
local Durability = Tool:GetAttribute("Durability")
local DefaultSizeX = 440
local SizeSpacings = Durability / DefaultSizeX

Gui.Parent = LocalPlayer.PlayerGui
Gui.Enabled = false

Tool.Equipped:Connect(function()
	
	Gui.Enabled = true
	
end)

Tool.Unequipped:Connect(function()

	Gui.Enabled = false

end)

Tool:GetAttributeChangedSignal("Durability"):Connect(function() --THIS IS THE MAIN FOCUS!!
	
	DurabilityBar.Size = UDim2.new(0, SizeSpacings * DurabilityBar, 0, 10)
	
end)

Thank you for your time and support!

2 Likes

SizeSpacings only seems to be defined at the start of the script, so for when the attribute changed signal fires, it still is just multiplying it by the same SizeSpacings value you defined at the start (the value of your variable Durability isn’t changing at all)

Tool:GetAttributeChangedSignal("Durability"):Connect(function()

	SizeSpacings = Tool:GetAttribute('Durability') / DefaultSizeX
	DurabilityBar.Size = UDim2.new(0, SizeSpacings * DurabilityBar, 0, 10)
	
end)

also keep in mind that this signal will only fire if you change the attribute’s value with

:SetAttribute('Durability',...)
3 Likes

oh wow i just realized i made a typo there

DurabilityBar.Size = UDim2.new(0, SizeSpacings * DurabilityBar, 0, 10)

I meant for the “SizeSpacings” as an increment, and it was supposed to be multiplied with durability, but I guess I wasn’t careful when pressing tab

also unfortunately, this suggestion you made didn’t quite work, thanks regardless

1 Like

the problem with your code is that SizeSpacings’ value was never going to change.

This is because Durability’s value is never going to change, since it only got defined once, at the start of the script:

Tool:GetAttribute doesnt listen for changes, it isnt the same as doing something like NumberValue.Value
In order to have a functionality like this you would have to do

local Durability = function() return Tool:GetAttribute("Durability") end

and

local SizeSpacings = function() return Durability() / DefaultSizeX end

and in the signal event, code this line like so:

Tool:GetAttributeChangedSignal("Durability"):Connect(function()
	
	DurabilityBar.Size = UDim2.new(0, SizeSpacings() * DurabilityBar, 0, 10)
	
end)
2 Likes

The durability does change via a server script, this is a separate piece of code Edit: I think I get what you mean now

1 Like

you misunderstand what I mean, the variable in your localscript named Durability was never going to change, the Durability attribute is going to change however.

These are not the same, when you define it as a variable with :GetAttribute() you are storing a copy of its value in that particular moment

3 Likes

Yeah I know, I figured a minute ago. Thank you for explaining to me lol, wouldn’t have realized it myself

2 Likes

Alright I just realized the signal isn’t running anything at all, just used a print() to test that.

task.wait(0.5)

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Gui = script.Parent
local MainFrame = Gui.MainFrame
local DurabilityBar = MainFrame.DurabilityBar
local Tool = Gui.Parent
local Durability = function() return Tool:GetAttribute("Durability") end
local DefaultSizeX = 440
local SizeSpacings = function() return Durability() / DefaultSizeX end

Gui.Parent = LocalPlayer.PlayerGui
Gui.Enabled = false

Tool.Equipped:Connect(function()
	
	Gui.Enabled = true
	
end)

Tool.Unequipped:Connect(function()

	Gui.Enabled = false

end)

Tool:GetAttributeChangedSignal("Durability"):Connect(function()
	
	print("e")
	
	DurabilityBar.Size = UDim2.new(0, SizeSpacings * DurabilityBar, 0, 10)

end)

Do you know why? Sorry for bothering you so much today.

1 Like

Hello, no It’s no issue at all.
as far as I’m aware Tool:GetAttributeChangedSignal only fires when changing the attribute with :SetAttribute.

Though other than relying on the client replication of this signal, you’re better off using a RemoteEvent fired from your serverscript

2 Likes

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