So, I am making a gun for my game but when I use it I get an error saying

Attempt to call index upvalue 'handle' (a nil value) on line 3

Here is the code:

local tool = script.Parent

local handle = tool:FindFirstChild("Handle")
local hole = tool:FindFirstChild("Hole")

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local player = players.LocalPlayer

local equipEvent = replicatedStorage:WaitForChild("EquipEvent")
local reloadEvent = replicatedStorage:WaitForChild("ReloadEvent")
local shootEvent = replicatedStorage:WaitForChild("ShootEvent")
local unequipEvent = replicatedStorage:WaitForChild("UnequipEvent")

local settings = tool:WaitForChild("Settings")

local bullets = settings:WaitForChild("Bullets")
 
local bullets = settings:WaitForChild("Bullets")
local cooldown = settings:WaitForChild("Cooldown")
local damage = settings:WaitForChild("Damage")
local maxBullets = settings:WaitForChild("MaxBullets")
local reloadTime = settings:WaitForChild("ReloadTime")

local debounce = false

tool.Equipped:connect(function(mouse)
	equipEvent:FireServer(handle.EquipAnimation)
	
	mouse.Button1Down:connect(function()
		if debounce == false then
			debounce = true
			if bullets.Value == 0 then
				reloadEvent:FireServer(bullets, maxBullets, reloadTime, handle.ReloadAnimation)
				
				wait(reloadTime.Value)
				
				debounce = false
			else
				local ray = Ray.new(hole.CFrame.p, (mouse.Hit.p - hole.CFrame.p).unit * 300)
				local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
				
				shootEvent:FireServer(part, position, hole, bullets, damage)
				
				wait(cooldown.Value)
				
				debounce = false
			end
		end
	end)
	
	tool.Unequipped:Connect(function()
		unequipEvent:FireServer()
	end)
end)

1 comment

0

Never mind I found a fix for it!