So basically, I'm making a GUI that will display how much ammo is left in your gun. I'm using two IntValues inside of the gun, "Ammo" and "MaxAmmo".

When I test to see if the GUI is visible or not, I get the error "Players.JoshGamingHQ1.PlayerGui.AmmoScreen.Frame.TextLabel.LocalScript:1: attempt to index a nil value".

This is the code:

local toolWithAmmo = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool").Ammo
local toolWithMaxAmmo = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool").MaxAmmo

local function update()
	
	script.Parent.Text = toolWithAmmo.Value.." / "..toolWithMaxAmmo.Value
	
end

while true do
	wait()
	if toolWithAmmo ~= nil and toolWithMaxAmmo ~= nil then
		script.Parent.Visible = true
		update()
	else
		script.Parent.Visible = false
		
	end
end

This script is a Local Script inside of a TextLabel.

(The issue may be obvious and I don't see it, I'm fairly new to Roblox Lua scripting.)

2 comments

0

What I do, is put the gui in the tool, and the local script in the tool as well, the local script handles the gui change. It will parent the gui to playergui when equipped, parent it back to the tool when unequipped. The int values are in the tool and from the tool you can easily listen for changed events.

0

So if I were to put the script and GUI inside the tool, would that mean I would have to copy the script and GUI for each tool?