Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Gun Ammo Does Not Update After Unequip?

Whenever I unequip and re-equip the gun, the GUI shows up, but the text appears to be wrong.

Updating Ammo:

--//Variables
local plr = game.Players.LocalPlayer
local plrgui = plr:WaitForChild("PlayerGui")
local AmmoGui = plrgui:WaitForChild("AmmoGui")
local Frame = AmmoGui:WaitForChild("Frame")
local tool = script.Parent
local config = tool:WaitForChild("Config")
local ammo = config:WaitForChild("Ammo")
local clips = config:WaitForChild("Clips")
local display = Frame:WaitForChild("Display")

--//Function(s)
local function Update()
	display.Text = ammo.Value .. "/" .. clips.Value
	wait()
end

--//Events
tool.Activated:connect(Update)
ammo.Changed:connect(Update)
clips.Changed:connect(Update)

tool.Equipped:connect(function()
	display.Visible = true
	Update()
end)

Cloning Ammo GUI:

local plr = game.Players.LocalPlayer
local playerGui = plr:WaitForChild("PlayerGui")
local controlGuiClass = script.Parent.Parent.AmmoGui
local tool=script.Parent.Parent

tool.Equipped:connect(function()
	local CGClone = controlGuiClass:Clone()
	CGClone.Parent = playerGui
	CGClone.Frame.Display.Visible = true
end)

--this is in ControlsGui local script
tool.Unequipped:Connect(function()
	if playerGui:FindFirstChild("AmmoGui") then -- B/c it'll check the original variable, which you'll have to have the script
		playerGui.AmmoGui:Destroy()
	end
end)

GIF

Error

Hopefully there's a way to fix this..

-LukeGabrieI

1 comment

0

The gun looks really cool.

1 answer

✓ Accepted answer

So what is happening here is that in the first script your AmmoGui reference becomes outdated. This happens because you delete the GUI with:

        playerGui.AmmoGui:Destroy()

What I would suggest to avoid this is to re-use the GUI and just make it's components invisible.

tool.Equipped:connect(function()
    local ammogui = playerGui:FindFirstChild("AmmoGui")
    if ammogui == nil then
        ammogui = controlGuiClass:Clone()
        ammogui.Parent = playerGui
    end
    ammogui.Frame.Display.Visible = true
end)

--this is in ControlsGui local script
tool.Unequipped:Connect(function()
    if playerGui:FindFirstChild("AmmoGui") then -- B/c it'll check the original variable, which you'll have to have the script
        playerGui.AmmoGui.Frame.Display.Visible = false
    end
end)

1 comment

0

GENIUS

This discussion is preserved as it was. New answers, comments, and votes are closed.