Hi, I'm sort of new to scripting, and I'm trying to make a weapon that can shoot. The problem is that in my output, it says "Attempt to connect failed: passed value not a function" and whenever after that, it says "attempt to call nil value". I have isolated the problem to the script below, but I can't find what's wrong with it.

local function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
	shoot = false 
	reload = false
	CloneAmmo.Text = ("...")
	CloneAmmo.Parent.Parent.TextBox.TextTransparency = 0
	CloneAmmo.Parent.Parent.TextBox.Text = "Reloading"
	Gun.Mag.Transparency = 1
	if stored.Value > mag.Value and ammoInClip.Value == 0 then
		stored.Value = stored.Value - (mag.Value - 1)
		ammoInClip.Value = mag.Value - 1
	end
	if stored.Value > mag.Value and ammoInClip.Value > 0 then
		stored.Value = stored.Value - mag.Value
		ammoInClip.Value = mag.Value
	end
	Gun.Mag.Transparency = 0
	wait(2.5)
	ammoInClip.Value = mag.Value
	CloneAmmo.Parent.Parent.TextBox.TextTransparency = 1
	CloneAmmo.Text = ammoInClip.Value
	CloneStored = stored.Value
	if stored.Value <= 0 then
	reload = false
	elseif stored.Value > 0 then
	reload = true
	end
	shoot = true
	end
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
shooting() 

Here is the whole script if needed

Gun = script.Parent
tool = script.Parent
ammoInClip = Gun:WaitForChild("Ammo")
mag = Gun:WaitForChild("MagSize")
stored = Gun:WaitForChild("StoredAmmo")
Label = script.ScreenGui.Frame.Ammo
Gui = script.ScreenGui
GuiClone = Gui:Clone()
GuiClone.Parent = game.Players.LocalPlayer.PlayerGui
CloneFrame = GuiClone.Frame
CloneAmmo = CloneFrame.Ammo
CloneStored = CloneFrame.Stored
CloneAmmo.Text = ammoInClip.Value
CloneStored.Text = stored.Value
fire = Gun.Handle.Fire
click = Gun.Handle.Click
reloadsound = Gun.Handle.Reload
local player = game:GetService("Players").LocalPlayer
shoot = true
print(ammoInClip.Value)
function shooting()
Gun.Equipped:connect(function(mouse)
	print("Tool equipped")
	mouse.Button1Down:connect(function()
	if shoot == true then
		fire:Play()
		ammoInClip.Value = ammoInClip.Value - 1
		print(ammoInClip.Value)
	mouse.Button1Down:connect(function()
	if ammoInClip.Value <= 0 then
		shoot = false 
		click:Play()
	end
	end)
		print("Mouse pressed")
		local ray = Ray.new(tool.Handle.CFrame.p, (tool.Main.CFrame.rightVector).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("New Yeller")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
 
		local distance = (tool.Main.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(tool.Main.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
 
		game:GetService("Debris"):AddItem(beam, 0.1)
 
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
 
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
 
			if humanoid then
				humanoid:TakeDamage(30)
			end
		end
		end
	end)
end)

reload = true
local function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
	shoot = false 
	reload = false
	CloneAmmo.Text = ("...")
	CloneAmmo.Parent.Parent.TextBox.TextTransparency = 0
	CloneAmmo.Parent.Parent.TextBox.Text = "Reloading"
	Gun.Mag.Transparency = 1
	if stored.Value > mag.Value and ammoInClip.Value == 0 then
		stored.Value = stored.Value - (mag.Value - 1)
		ammoInClip.Value = mag.Value - 1
	end
	if stored.Value > mag.Value and ammoInClip.Value > 0 then
		stored.Value = stored.Value - mag.Value
		ammoInClip.Value = mag.Value
	end
	Gun.Mag.Transparency = 0
	wait(2.5)
	ammoInClip.Value = mag.Value
	CloneAmmo.Parent.Parent.TextBox.TextTransparency = 1
	CloneAmmo.Text = ammoInClip.Value
	CloneStored = stored.Value
	if stored.Value <= 0 then
	reload = false
	elseif stored.Value > 0 then
	reload = true
	end
	shoot = true
	end
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
shooting()

5 comments

0

I'm not going to read all of this because whoo doggie, but your problem is likely that onKeyPress hasn't been defined yet when you try to connect it. I can't really tell because of the lack of indentation, but it seems like it's defined in shooting, and it's a local function.

0

So, all you'd need to do to fix it is move where it's defined.

0

Or just move the onKeyPress connection to inside of the shooting function. Whatever floats your boat.

0

It seems like the function onKeyPress only exists withing the shooting function so it will be nil and not a function.

0

oh thanks!