Help on making gun shoot

I’m making an fps game for fun, and I ran into this problem. my gun is a mesh, and I have it welded in my hand. it is inside the character but its a local script when I check the gun the local script is gone here is my local script:

localscript inside the gun

local plrs = game.Players
local reps = game.ReplicatedStorage
local plr = plrs.LocalPlayer

local mouse = plr:GetMouse()

local gun = script.Parent

mouse.Button1Down:Connect(function()
	if mouse.Target ~= nil then
		reps.Shoot:FireServer(mouse.Hit.Position, false)
	else
		reps.Shoot:FireServer(mouse.Hit.Position, true)
	end
end)

serverscript in scs

local plrs = game.Players
local reps = game.ReplicatedStorage
local tws= game:GetService("TweenService")

local gunrange = 500-- in studs

reps.Shoot.OnServerEvent:Connect(function(plr,mousepos,target)
	local gun = plr.Character:FindFirstChild('Gun')
	local dmg = gun.Damage.Value
	
	if gun == nil  then end
	
	local direction = (mousepos - gun.ShootPart.Position) * gunrange
	
	local ray = RaycastParams.new()
	ray.FilterDescendantsInstances = {plr.Character}
	ray.FilterType = Enum.RaycastFilterType.Exclude
	
	local result = workspace:Raycast(gun.ShootPart.Position, direction, ray)
	
	local bullet = reps.Bullet:Clone()
	bullet.CFrame = gun.ShootPart.CFrame
	bullet.Parent = workspace
	
	local bduration = 0.3 * ((direction/gunrange).Magnitude)/50
	
	tws:Create(bullet, TweenInfo.new(bduration), {CFrame = CFrame.new(mousepos)}):Play()
	
	if not target then
		task.delay(bduration, function()
			bullet:Destroy()
		end)
	else
		task.delay(1, function()
			bullet:Destroy()
		end)
	end
	
	if result == nil then return end
		
	local hum = result.Instance.Parent:FindFirstChild("Humanoid")
	
	if hum == nil then return end
	
	task.wait(bduration)
	
	hum.Health -= dmg
end)
1 Like

Is the gun you are using a tool?

1 Like

no it is a mesh welded to my hand

I think the problem is the local script when I’m in game and check the children of the gun I don’t see the local script and it is inside the gun

1 Like

ayyyy I was right I moved it outside of the gun into the scharscripts and changed the variables a bit and it works

It’s good that you found the solution, however, unless your game is in a really nieche scenario, you should make it a tool, and put the local script in it, then put it in starterpack if you need players to spawn in with it.

You can use a plugin like Tool Grip Editor to adjust the grip so you hold it right. you don’t need to weld it to your hand.
image

I think that plugin costs 100 robux though, you could use this free version with 1,000+ likes, I just checked it out and it appears to be safe, and it works about the same just not as nice, and missing some QOL.

You don’t need to grab the mouse and detect when it’s clicked, you can simply use Tool.Activated() and place all the logic in that connection, that runs whenever the player clicks while holding the tool :slight_smile:

yes I have tried that exact thing but when I played running animations it made my right arm hold the tool and made my left arm do the animation how I fix it?

When you were animating with the tool, you likely rigged the gun to the wrong arm.

Making the priority action could help as well.

I didn’t rig ir with the gun I am supposed to??
im so dum ty bro ill try it

1 Like

The running animation should overlap the tool, you shouldn’t really need to animate with it. do you remember what priority you set the animation to?

no I don’t but I can use remake it it didn’t look too god anyways

how do I make it have a higher priority than the gun?

Do you use moon animator or the default roblox animator?

If you use the default, somewhere around the save tab it should show a button called Change Animation Priority, set it to movement.

you should not need to animate with the gun at all as long as the gun itself isn’t playing any animations, if you have the gun set to play animations set those to action and it will override the running animation, so I suggest you only animate the arms for aiming the gun, nothing else. You do not need to animate the legs or torso.

If you use moon animator, just press file and change it from there.

the default Roblox avatar animator
my moon animator broke

ok so I don’t have any gun animations but I can’t animate my legs or torso?? cuz I wanna make a walk animation so I have to move my legs

ok I made the run animation and set it to movement so this will override the gun default hand position?

Okay basically this is what your animations should be:

For running, you can just set it to movement, you can animate any limbs, limbs you do not animate will have other animations of higher or the same priority animate them if there are multiple animations all at once.

For aiming, you only need to animate the arms, either one arm or both, if you want to animate the legs and torso, I am gonna assume you are trying to make an aiming animation where you stand still, if that is the case you can use code to play the animation for aiming while standing still and else just play the normal aiming animation.

Running should be priority Movement, Aiming should be Priority Action.

You do not need to animate the running animation with the gun, it will override the gun as long as you are animating the arm the gun is supposed to be in.