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

Gun is apparently not working for some reason due to some error?

So the problem is it keeps giving me the error Players.Player1.Backpack.Pistol.Bullets:31: attempt to index upvalue 'gui' (a function value) I obviously have no clue what this means. I have never scripted a gun before(obviously with the code) so I am kind of naïve on things. If you find another problem besides this please help also! `

local script:

-- Tool
local tool = script.Parent
-- Values 
local Owner = tool:WaitForChild("Owner")
local Max = tool:WaitForChild("Maxbullets")
local Ammo = tool:WaitForChild("Ammo")
-- Server scripts
local Damage = tool.Damage
--Local Scripts
local mouse = tool:WaitForChild("MouseIcons")
local mouse2 = game.Players.LocalPlayer:GetMouse()
local bullets = tool.Bullets
--Player
local player = Owner.Value
local rplayer = tool.Parent
--Gui
local gui = tool:WaitForChild("BulletGUI")
local back = gui:WaitForChild("Background")
local load = back:WaitForChild("BulletLoad")
-- Handle
local handle = tool.Handle
--Services
local uis = game:GetService("UserInputService")
local debris = game:GetService("Debris")
--Debounce 
local debounce1 = false
local debounce2 = false


tool.Equipped:connect(function(equip)
	gui()
	print("Cloned")
	equip.Button1Down:connect(function()
		print("Yes")
		if debounce2 == false then
			print("Hello")
	     if Ammo.Value > 0 then
		print("Hello")
         Ammo.Value = Ammo.Value - 1
         bullet()
         wait(0.4)
         update()
         print("Changed")
         wait(1)
        debounce2 = false
	
		uis.InputBegan:connect(function(key)
			if key.KeyCode == Enum.KeyCode.R then
				if debounce1 == false then
				local find = rplayer.Parent.PlayerGui:FindFirstChild("BulletGUI"):FindFirstChild("Background"):FindFirstChild("BulletLoad")
				if find then
					debounce1 = true
					if Ammo.Value < Max.Value then
					Ammo.Value = Max.Value
					wait(1)
					debounce1 = false
			    else
				   return end
					               
							
						end
					 end
	              end
	          end)
			end
		end
	end)
end)
		
function bullet()
	local part = Instance.new("Part")
         part.formFactor = "Symmetric"
         part.Shape = "Ball"
         part.Parent = tool.Handle
         part.CanCollide = false
         debounce2 = true
         part.Anchored = false
         part.Size = Vector3.new(1,1,1)
         part.TopSurface = "Smooth"
         part.BottomSurface = "Smooth"
         part.Transparency = 1
         part.Name = "Bullet"
         part.BrickColor = BrickColor.new("Smoky grey")
         part.CFrame = tool.Handle.CFrame * CFrame.new(0,1.5,0)
         print("Clone")
         part.Touched:connect(function(hit)
           script.Parent:WaitForChild("Event"):FireServer(hit.Parent)
           print("Fired")
            
          end)
         local bv = Instance.new("BodyVelocity")
     bv.Parent = part
     bv.Velocity = (game.Players.LocalPlayer.Character.Humanoid.TargetPoint - part.Position).unit * 200
end

function gui()
	local clone = gui:Clone()
	clone.Parent = rplayer.Parent.PlayerGui
end
function update()
	load.Text = "Ammo: "..Ammo.Value.."/50"
end



tool.Unequipped:connect(function()
	rplayer.Parent.PlayerGui:FindFirstChild("BulletGUI"):Destroy()
end)
Ammo.Changed:connect(update)

1 answer

✓ Accepted answer

A variable and a function cannot have the same name, as the one defined later on in the script will overwrite the first one. To fix this, simply give the gui function a different name.

-- Tool
local tool = script.Parent
-- Values 
local Owner = tool:WaitForChild("Owner")
local Max = tool:WaitForChild("Maxbullets")
local Ammo = tool:WaitForChild("Ammo")
-- Server scripts
local Damage = tool.Damage
--Local Scripts
local mouse = tool:WaitForChild("MouseIcons")
local mouse2 = game.Players.LocalPlayer:GetMouse()
local bullets = tool.Bullets
--Player
local player = Owner.Value
local rplayer = tool.Parent
--Gui
local gui = tool:WaitForChild("BulletGUI")
local back = gui:WaitForChild("Background")
local load = back:WaitForChild("BulletLoad")
-- Handle
local handle = tool.Handle
--Services
local uis = game:GetService("UserInputService")
local debris = game:GetService("Debris")
--Debounce 
local debounce1 = false
local debounce2 = false


tool.Equipped:connect(function(equip)
    makeGUI()
    print("Cloned")
    equip.Button1Down:connect(function()
        print("Yes")
        if debounce2 == false then
            print("Hello")
         if Ammo.Value > 0 then
        print("Hello")
         Ammo.Value = Ammo.Value - 1
         bullet()
         wait(0.4)
         update()
         print("Changed")
         wait(1)
        debounce2 = false

        uis.InputBegan:connect(function(key)
            if key.KeyCode == Enum.KeyCode.R then
                if debounce1 == false then
                local find = rplayer.Parent.PlayerGui:FindFirstChild("BulletGUI"):FindFirstChild("Background"):FindFirstChild("BulletLoad")
                if find then
                    debounce1 = true
                    if Ammo.Value < Max.Value then
                    Ammo.Value = Max.Value
                    wait(1)
                    debounce1 = false
                else
                   return end


                        end
                     end
                  end
              end)
            end
        end
    end)
end)

function bullet()
    local part = Instance.new("Part")
         part.formFactor = "Symmetric"
         part.Shape = "Ball"
         part.Parent = tool.Handle
         part.CanCollide = false
         debounce2 = true
         part.Anchored = false
         part.Size = Vector3.new(1,1,1)
         part.TopSurface = "Smooth"
         part.BottomSurface = "Smooth"
         part.Transparency = 1
         part.Name = "Bullet"
         part.BrickColor = BrickColor.new("Smoky grey")
         part.CFrame = tool.Handle.CFrame * CFrame.new(0,1.5,0)
         print("Clone")
         part.Touched:connect(function(hit)
           script.Parent:WaitForChild("Event"):FireServer(hit.Parent)
           print("Fired")

          end)
         local bv = Instance.new("BodyVelocity")
     bv.Parent = part
     bv.Velocity = (game.Players.LocalPlayer.Character.Humanoid.TargetPoint - part.Position).unit * 200
end

function makeGUI()
    local clone = gui:Clone()
    clone.Parent = rplayer.Parent.PlayerGui
end
function update()
    load.Text = "Ammo: "..Ammo.Value.."/50"
end



tool.Unequipped:connect(function()
    rplayer.Parent.PlayerGui:FindFirstChild("BulletGUI"):Destroy()
end)
Ammo.Changed:connect(update)

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