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)
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! 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.
local Mouse = game.Players.LocalPlayer:GetMouse()
local Camera = workspace.CurrentCamera
local ray = workspace:RayCast(Camera.CFrame.Position,Mouse.UnitRay*2048)
print(ray.Instance)
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.
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)
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])
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.
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.
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
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