Ammo Gui's Text Not Changing to Mirror NumberValue

I am creating a cannon tool, and I would like for the textlabel’s text to change whenever the ammo NumberValue changes, which will happen when the player fires the weapon.

For some reason, the text simply does not change. The localscript can read the value (it’s being set serverside), and I know this because it works correctly when it does the same thing except when unequipping and then reequipping the tool. Though when I attempt to do it mid-script, it doesn’t do anything.

There are no errors in the console, and I have tried every alternative way I can think of, yet it still doesn’t work.

Script (I have marked the part that doesn’t work in a code comment):

local Players = game:GetService("Players")
local holding = false

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:Wait()
end
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local ToolAnimation = Instance.new("Animation")
ToolAnimation.AnimationId = "rbxassetid://9157515489"
local ToolAnimationTrack = animator:LoadAnimation(ToolAnimation)
local Cooldown = false



script.Parent.Activated:Connect(function()
local ammo = script.Parent.Ammo.Value
	if Cooldown == false and ammo > 0 then
		Cooldown = true
		ToolAnimationTrack:Play()
		--the problem occurs here
		local uitext = game.Players.LocalPlayer.PlayerGui:FindFirstChild("AmmoGui").Frame.Ammo.Text
		uitext = ammo - 1
		wait(5)
		Cooldown = false
	end
end)



script.Parent.Equipped:Connect(function()
	holding = true
	local GuiClone = script.AmmoGui:Clone()
	GuiClone.Parent = player.PlayerGui
	local ammo = script.Parent.Ammo.Value
	GuiClone.Frame.Ammo.Text = ammo
end)


script.Parent.Unequipped:connect(function()
	holding = false
	ToolAnimationTrack:Stop()
	game.Players.LocalPlayer.PlayerGui:FindFirstChild("AmmoGui"):Destroy()
end)

Any help or explanation to why this is happening would be much appreciated!

1 Like

Try this out, see what it prints.

local PS = game:GetService("Players")

script.Parent.Activated:Connect(function()
	local ammo = script.Parent.Ammo.Value

	if Cooldown == false and ammo > 0 then
		Cooldown = true
		ToolAnimationTrack:Play()
		--the problem occurs here
		local ammoGui = PS.LocalPlayer.PlayerGui:FindFirstChild("AmmoGui")
		
		if not ammoGui then
			warn("ammogui is nil")
			return
		end
		
		local uitext: TextLabel = ammoGui.Frame:FindFirstChild("Ammo")
		
		if not uitext then
			warn("uitext is nil")
			return
		end
		
		if uitext then
			uitext.Text = tostring(ammo - 1)
			warn("TEXT: " .. uitext.Text)
			task.wait(5)
			Cooldown = false
		end
	end
end)

Doesnt the text need to be in quotes?

Upon trying this again, it works the second time. Maybe it was just a one-off bug that it was printing the correct value, but not changing it.

No, then it would say it in the literal sense, reading “ammo - 1”. I want it to be the current value, which does not need to be in quotes.

1 Like

Sorry i thought u wanted it written out lol.

Use .Changed, Make another local script
And,

YourTextLabelSpot = script.Parent.TextLabel

YourNumberValueSpot = game.Players.LocalPlayer.NumberValue

YourNumberValueSpot.Changed:Connect(function()

YourTextLabelSpot.Text = YourNumberValueSpot.Value

end)

try to:

script.Parent.Equipped:Connect(function()
	holding = true
	local GuiClone = script.AmmoGui:Clone()
	GuiClone.Parent = player.PlayerGui
	local ammo = script.Parent.Ammo
	GuiClone.Frame.Ammo.Text = ammo.Value
    print(ammo.Value)
end)