Help with Gun script

  1. 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.
  2. The debounce doesn’t work and I’m forced to add the local keywords in the shooting code.
  3. Animation won’t play.
  4. The muzzle flash doesn’t work.
  5. 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)

Can you show me the script you’re firing a signal to.

2 Likes

Can’t you also preset the flash to false instead of using a function to make it false?

1 Like

I guess so I thought it would force it to be like that forever but maybe not.

It this:

local RS = game:GetService("ReplicatedStorage")

RS.Shot.OnServerEvent:Connect(function(player, target)
	target:FindFirstChild("Humanoid"):TakeDamage(20)
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:

https://www.youtube.com/watch?v=e3z91RqZPAk

https://www.youtube.com/watch?v=MQCArb1XiBc&t=656s

I’m not sure why this would be helping my problem.

It’s hard to fix this script because it doesn’t follow the algorithm. This is not a script you would use to make a gun for a game.

1 Like