I am trying to write a simple gun script with the usage of RemoteEvents, however the script is experiencing the error "attempt to index nil with ‘Hit’ ". I’ve tried to search up what could cause this sort of error, including on the Devforum but none are specifically like this one. The gun worked before I implemented the RemotEvent and I have no idea what is causing the issue.
So far I’ve tried to see if I wrote the FireServer part wrong but so far it seems to work according to the sources I’ve used. I also tried to replace the mouse.Hit.pos with the CFrame and Position of where the mouse is poiting but it instead gave me the error that it wasn’t a Vector3 Value, so I decided to stick to this variant instead, unless I find another, better variant.
I am aware my script isn’t exactly refined but I’m only looking for a solution to said error, not criticism about how I’ve written it.
(there is also a bug with the ammo script but thats something I’ll look into afterwards)
Main Script (LocalScript)
GUI = game.Players.LocalPlayer.PlayerGui.GunUI
local mouse = game.Players.LocalPlayer:GetMouse()
local UserInputService = game:GetService("UserInputService")
local FireRate = script.Parent.FireRate.Value
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local Ammo = script.Parent.ReserveAmmo.Value
local Magazine = tool.Magazine.Value
local CanShoot = true
FireRateCooldown = true
Damage = 11
local Fire = script.Parent.Fire
tool.Equipped:connect(function(mouse)
GUI.AmmoLabel.Visible = true --- Ammo and GUI
GUI.TextLabel.Visible = true
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
UserInputService.InputBegan:connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.R then --- Manual Reload
if CanShoot == false then
return
end
if tool.ReserveAmmo.Value <= 0 and tool.Magazine.Value <= 0 then
return
end
if tool.Magazine.Value == 10 then
return
end
CanShoot = false
tool.Reload:Play()
wait(2)
if tool.ReserveAmmo.Value > 0 and tool.ReserveAmmo.Value < 10 and tool.Magazine.Value <= 0 then
tool.Magazine.Value = tool.ReserveAmmo.Value
tool.ReserveAmmo.Value = 0
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
CanShoot = true
else if tool.Magazine.Value > 0 and tool.Magazine.Value < 10 then
tool.ReserveAmmo.Value = tool.ReserveAmmo.Value - ( 10 - tool.Magazine.Value)
tool.Magazine.Value = 10
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
CanShoot = true
else
tool.ReserveAmmo.Value = tool.ReserveAmmo.Value - (tool.Magazine.Value + 10)
tool.Magazine.Value = 10
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
CanShoot = true
end
end
end
end
end)
mouse.Button1Down:connect(function(mouse) --- Firing
if CanShoot == false then
return
end
if tool.ReserveAmmo.Value <= 0 and tool.Magazine.Value <= 0 then
return
end
if tool.Magazine.Value <= 0 and Ammo > 0 then --- Automatic Reload
CanShoot = false
tool.Reload:Play()
wait(2)
if tool.ReserveAmmo.Value > 0 and tool.ReserveAmmo.Value < 10 and tool.Magazine.Value <= 0 then
tool.Magazine.Value = tool.ReserveAmmo.Value
tool.ReserveAmmo.Value = 0
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
CanShoot = true
else
tool.ReserveAmmo.Value = tool.ReserveAmmo.Value - (tool.Magazine.Value + 10)
tool.Magazine.Value = 10
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
CanShoot = true
end
else if FireRateCooldown == true then
Fire:FireServer(mouse.Hit.pos, mouse) --- Signal the other script to fire the gun.
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
end
end
end)
end)
tool.Unequipped:Connect(function()
GUI.AmmoLabel.Visible = false
GUI.TextLabel.Visible = false
end)
Firing Script (Regular Script)
local UserInputService = game:GetService("UserInputService")
local FireRate = script.Parent.FireRate.Value
tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local Ammo = script.Parent.ReserveAmmo.Value
local Magazine = tool.Magazine.Value
local CanShoot = true
FireRateCooldown = true
Damage = 11
Fire = script.Parent.Fire
Fire.OnServerEvent:connect(function(mouse)
tool.Shoot:Play()
tool.Magazine.Value = tool.Magazine.Value -1
local ray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.pos - tool.Barrel.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Institutional white")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Barrel.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")
FireRateCooldown = false
wait(FireRate)
FireRateCooldown = true
end
if humanoid then
humanoid:TakeDamage(Damage)
FireRateCooldown = false
wait(FireRate)
FireRateCooldown = true
end
end
end)
GUI = game.Players.LocalPlayer.PlayerGui.GunUI
local mouse = game.Players.LocalPlayer:GetMouse()
local UserInputService = game:GetService("UserInputService")
local FireRate = script.Parent.FireRate.Value
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local Ammo = script.Parent.ReserveAmmo.Value
local Magazine = tool.Magazine.Value
local CanShoot = true
FireRateCooldown = true
Damage = 11
local Fire = script.Parent.Fire
tool.Equipped:connect(function(mouse)
GUI.AmmoLabel.Visible = true --- Ammo and GUI
GUI.TextLabel.Visible = true
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
UserInputService.InputBegan:connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.R then --- Manual Reload
if CanShoot == false then
return
end
if tool.ReserveAmmo.Value <= 0 and tool.Magazine.Value <= 0 then
return
end
if tool.Magazine.Value == 10 then
return
end
CanShoot = false
tool.Reload:Play()
wait(2)
if tool.ReserveAmmo.Value > 0 and tool.ReserveAmmo.Value < 10 and tool.Magazine.Value <= 0 then
tool.Magazine.Value = tool.ReserveAmmo.Value
tool.ReserveAmmo.Value = 0
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
CanShoot = true
else if tool.Magazine.Value > 0 and tool.Magazine.Value < 10 then
tool.ReserveAmmo.Value = tool.ReserveAmmo.Value - ( 10 - tool.Magazine.Value)
tool.Magazine.Value = 10
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
CanShoot = true
else
tool.ReserveAmmo.Value = tool.ReserveAmmo.Value - (tool.Magazine.Value + 10)
tool.Magazine.Value = 10
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
CanShoot = true
end
end
end
end
end)
mouse.Button1Down:connect(function(mouse) --- Firing
if CanShoot == false then
return
end
if tool.ReserveAmmo.Value <= 0 and tool.Magazine.Value <= 0 then
return
end
if tool.Magazine.Value <= 0 and Ammo > 0 then --- Automatic Reload
CanShoot = false
tool.Reload:Play()
wait(2)
if tool.ReserveAmmo.Value > 0 and tool.ReserveAmmo.Value < 10 and tool.Magazine.Value <= 0 then
tool.Magazine.Value = tool.ReserveAmmo.Value
tool.ReserveAmmo.Value = 0
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
CanShoot = true
else
tool.ReserveAmmo.Value = tool.ReserveAmmo.Value - (tool.Magazine.Value + 10)
tool.Magazine.Value = 10
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
CanShoot = true
end
else if FireRateCooldown == true then
Fire:FireServer(mouse.Hit.p) --- Signal the other script to fire the gun.
GUI.AmmoLabel.Text = tool.Magazine.Value .. "/" .. tool.ReserveAmmo.Value
end
end
end)
end)
tool.Unequipped:Connect(function()
GUI.AmmoLabel.Visible = false
GUI.TextLabel.Visible = false
end)
Firing Script
local UserInputService = game:GetService("UserInputService")
local FireRate = script.Parent.FireRate.Value
tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local Ammo = script.Parent.ReserveAmmo.Value
local Magazine = tool.Magazine.Value
local CanShoot = true
FireRateCooldown = true
Damage = 11
Fire = script.Parent.Fire
Fire.OnServerEvent:connect(function(player, mouse)
print(player.Name)
tool.Shoot:Play()
tool.Magazine.Value = tool.Magazine.Value -1
local ray = Ray.new(tool.Barrel.CFrame.p, (mouse - tool.Barrel.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Institutional white")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Barrel.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")
FireRateCooldown = false
wait(FireRate)
FireRateCooldown = true
end
if humanoid then
humanoid:TakeDamage(Damage)
FireRateCooldown = false
wait(FireRate)
FireRateCooldown = true
end
end
end)