It doesn’t cause damage and the problem is in this script when I started it worked but once I added more features it stopped and in all that time I didn’t touch the server side of things.
The debounce doesn’t work and I’m forced to add the local keywords in the shooting code.
Animation won’t play.
The muzzle flash doesn’t work.
The camera mode code has to be on the top and I don’t like that.
--Vars
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local RS = game:GetService("ReplicatedStorage")
local debounce = false
--FPS
script.Parent.Equipped:Connect(function()
player.CameraMode = Enum.CameraMode.LockFirstPerson
end)
script.Parent.Unequipped:Connect(function()
player.CameraMode = Enum.CameraMode.Classic
script.Parent.MuzzleFlash.MuzzleEffect.Enabled = false
end)
--Load
local Character = player:WaitForChild("Character")
local hum = Character:WaitForChild("Humanoid")
local Flash = script.Parent:WaitForChild("MuzzleFlash").MuzzleEffect
--Flash thing stop idk
function stopFlash()
Flash.Enabled = false
end
stopFlash()
--Recoil
local recoil = script.Parent.Recoil
local recoilTrack = hum:LoadAnimation(recoil)
--Fire
function fire()
local target = mouse.Target
if target then
if target.Parent:WaitForChild("Humanoid") then
RS.Shot:FireServer(target.Parent)
print("Hit")
end
script.Parent.shot:Play()
recoilTrack:Play()
end
end
--Shooting
script.Parent.Activated:Connect(function()
if debounce == false then
local debounce = true
Flash.Enabled = true
fire()
wait(0.2)
Flash.Enabled = false
local debounce = false
end
end)
--Vars
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local RS = game:GetService("ReplicatedStorage")
local debounce = false
local Character = player:WaitForChild("Character")
local hum = Character:WaitForChild("Humanoid")
local Flash = script.Parent:WaitForChild("MuzzleFlash").MuzzleEffect
local recoil = script.Parent.Recoil
local recoilTrack = hum:LoadAnimation(recoil)
local function fire()
local target = mouse.Target
print(target)
--You may also be able to use mouse.Hit and find whatever the mouse hits.
if target then
if target.Parent:WaitForChild("Humanoid") then
RS.Shot:FireServer(target.Parent)
print("Hit")
end
--What is shot?
script.Parent.shot:Play()
recoilTrack:Play()
end
end
script.Parent.Equipped:Connect(function()
player.CameraMode = Enum.CameraMode.LockFirstPerson
end)
script.Parent.Unequipped:Connect(function()
player.CameraMode = Enum.CameraMode.Classic
script.Parent.MuzzleFlash.MuzzleEffect.Enabled = false
end)
script.Parent.Activated:Connect(function()
if debounce == false then
local debounce = true
Flash.Enabled = true
fire()
wait(0.2)
Flash.Enabled = false
local debounce = false
end
end)
This isn’t a solution but I added some comments and edits that could help.
This method is definitely bad for guns, you should learn raycasting if you want to make a gun. Highly unrecommended, you can also make a cheap gun from this video.
Here are some posts and videos regarding vectors which are important in raycasting:
A tutorial from B Ricey on how to make a simple raycasting gun and instead of watching all those videos this Unity math video covers a lot about vectors: