Having trouble with making a gun system

So far I have had a pretty easy time making the gun system, I have gotten it to play animations and play audio but for the shooting part I have been stuck. What I want it to do is detect if you are clicking on a humanoid and if so remove health from that player. I don’t care to add anything complicated like raycasting at the moment I just want the basic shooting mechanics in the gun. So far what I have tried doing is looking for simple gun scripts and using their shooting system for mine but all were completely incompatible with my system.

local tool = script.Parent
local anim1 = Instance.new("Animation")
anim1.AnimationId = "http://www.roblox.com/Asset?ID=13694221900" --Animation ID
local anim2 = Instance.new("Animation")
anim2.AnimationId = "http://www.roblox.com/Asset?ID=13694242858"
local track
local gunDamage = 25

local function idleanim()
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim1)
	track.Priority = Enum.AnimationPriority.Action
	track.Looped = true
	track:Play()
end

local function fireanim()
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
	track.Priority = Enum.AnimationPriority.Action
	track.Looped = true
	track:Play()
end

local function toolEquipped()
	tool.Handle.Equip:Play()
	idleanim()
end

local function toolActivated()
	tool.Handle.Fire:Play()
	fireanim()
	tool.Handle.PointLight.Enabled = true
	wait(0.1)
	tool.Handle.PointLight.Enabled = false
	track:Stop()
	idleanim()
end

local function toolUnequipped()
	if track then
		track:Stop()
	end
end

tool.Equipped:Connect(toolEquipped)
tool.Activated:Connect(toolActivated)
tool.Unequipped:Connect(toolUnequipped)

Is the script you provided a server script or a client script? This will be crucial in determining the best approach for your system.

1 Like

It is a local script inside of a tool.

1 Like

I’ll be giving a recommended approach which assumes that you have a basic understanding over client-to-server communication using Remote Events and Functions. If there are any aspects of this post that are confusing or hard to understand, don’t hesitate to ask! :grin: I hope this helps!

You can use Player:GetMouse() in order to get the Mouse object. The Mouse has a property called Target, which gives you the Instance your mouse is currently pointing at.

You will need to create a new RemoteEvent and a Script in your Tool. This is going to handle the damaging of the player so other players can see the damage, too.

You can then check if your Mouse’s target is pointing at a model which contains a Humanoid like so:

local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()

function FindCharacterFromTarget()
	local Model = Mouse.Target:GetRootPart().Parent
	local Humanoid = Model:FindFirstChildOfClass('Humanoid')
	
	if Humanoid then
		return Model
	end
end

You can then use the returned model (which is your target’s character model) to fire the RemoteEvent you created earlier and apply damage on the server. You will need to make sure you handle the replication of that damage in the Script you wrote earlier.

1 Like

Do I do the damage in the:

if Humanoid then
	return Model
end
1 Like

No this is just to determine that your target is a player or NPC. You will need to handle damage in a separate server script.

1 Like

How would I go about doing that?

1 Like

Mouse.Target can not detect any other player parts for that you need to use raycasts. I suggest you use Mouse.UnitRay to see the player.

1 Like

I don’t really know how to do raycasts. How would I make that happen?

1 Like

Raycasting | Documentation - Roblox Creator Hub.

local Mouse = game.Players.LocalPlayer:GetMouse()
local Camera = workspace.CurrentCamera
local ray = workspace:RayCast(Camera.CFrame.Position,Mouse.UnitRay*2048)
print(ray.Instance)
1 Like

And I would put this in a normal script inside the tool correct?

1 Like

No you would put this inside a new unity project. OBVIOUSLY in the tool script. To be EXACT in the CLIENT tool script. Because you can only get the mouse in the client.

1 Like

If you’ve created a RemoteEvent as recommended in the response above, you can rename it to something like FireGun. You can then do this in your localscript:

local FireGun = script.Parent:FindFirstChild('FireGun')
FireGun:FireServer(Model)

You can then handle the fired event in a new Script, in which you handle the OnServerInvoke event. I sincerely recommend reading up on Remote events and functions as it will be pivotal to your future code.

An example of how to handle damaging could be this, though I recommend further polishing the code.

local FireGun = script.Parent:FindFirstChild('FireGun')
local Damage = 40

FireGun.OnServerEvent:Connect(function(Player, Target)
	local Humanoid = Target:FindFirstChild('Humanoid')
	if Humanoid then
		Humanoid:DealDamage(Damage)
	end
end)
1 Like

Yes it can. No raycasting is required for detecting other players’ characters.

1 Like

I am sure it can’t because most of the time using mouse.Target doesn’t work. And also I think using mouse.Target in a gun is impractical because you can easily wallbang (make bullets go through walls [in 3d only])

1 Like

For the FireGun:FireServer(Model) the localscript doesn’t quite understand as Model is established in the normal script. How would I fix that?

1 Like

So I inserted the 2nd part of this script in and it just stopped everything from working.

1 Like

If you are struggling to use Mouse.Target, you may be doing something incorrectly. I use it for my cursor system which displays information to the player based on its current target. This footage uses Mouse.Target

I agree Mouse.Target is not ideal for a gun system, but OP has specifically requested not to get into raycasting yet.

@OzQ_0

You should be defining the model in the LocalScript, as only the client can access the player’s mouse instance. You then pass the model you’ve established on the client to the server and deal damage there.

1 Like

Ah no no no I am saying it does work with humanoid but with actual players like other players it wont work unless OP is making a zombie game then I suppose use Mouse.Target

EDIT: wait sorry i am wrong. sorry for that!

2 Likes

Upon doing that I realized that at that point I was just pulling what was in the normal script into the localscript. What is the use of the serverscript if the localscript is doing the same job?

Localscript:

local tool = script.Parent
local anim1 = Instance.new("Animation")
anim1.AnimationId = "http://www.roblox.com/Asset?ID=13694221900" --Animation ID
local anim2 = Instance.new("Animation")
anim2.AnimationId = "http://www.roblox.com/Asset?ID=13694242858"
local track
local FireGun = script.Parent:FindFirstChild('FireGun')
local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local Model = Mouse.Target:GetRootPart().Parent
FireGun:FireServer(Model)
local Damage = 40

FireGun.OnServerEvent:Connect(function(Player, Target)
	local Humanoid = Target:FindFirstChild('Humanoid')
	if Humanoid then
		Humanoid:DealDamage(Damage)
	end
end)

local function idleanim()
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim1)
	track.Priority = Enum.AnimationPriority.Action
	track.Looped = true
	track:Play()
end

local function fireanim()
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
	track.Priority = Enum.AnimationPriority.Action
	track.Looped = true
	track:Play()
end

local function toolEquipped()
	tool.Handle.Equip:Play()
	idleanim()
end

local function toolActivated()
	tool.Handle.Fire:Play()
	fireanim()
	tool.Handle.PointLight.Enabled = true
	wait(0.1)
	tool.Handle.PointLight.Enabled = false
	track:Stop()
	idleanim()
end

local function toolUnequipped()
	if track then
		track:Stop()
	end
end

tool.Equipped:Connect(toolEquipped)
tool.Activated:Connect(toolActivated)
tool.Unequipped:Connect(toolUnequipped)

Serverscript:

local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()

function FindCharacterFromTarget()
	local Model = Mouse.Target:GetRootPart().Parent
	local Humanoid = Model:FindFirstChildOfClass('Humanoid')

	if Humanoid then
		return Model
	end
end