Hey all, i’m a beginner scripter, and i’m very confused on how I would make a gun. I figured out raycasting, and thought it would be easy to make when I learn that, but it has proved to be quite difficult to figure out how I am able to make one. Please help me out!
There are tons of ways to make a gun, and depending on the type of system, difficulty varies, theres a ton of tutorials on the dev forum on how to make a gun, try to find one you think is right for your game and follow it.
You shouldn’t focus on creating a FPS system at first. Here’s how you could try to create a gun tool instead:
-
First of all, go to workspace and create a tool, and under it create a part, and name it ‘Handle’. Handle is the main part of the tool and is the part that a character can be seen holding. You can customize it to however you like.
-
Second, create a local script, a server script, and a remote event under that tool.
-
Go into the local script and get the tool, and get an event for tool.Activated. It should look something like this:
local gun = script.Parent --You can just get the parent of the script
gun.Activated:Connect(function()
-- The code for when the gun is fired will go here
end)
- Now, we need to handle the shooting! We can go multiple ways about doing this: we can use the player’s mouse, or we can use the handle’s lookvector, or some other method. I’m going to be using the player’s mouse.
We are going to make it so that every time the gun is activated, a ray is fired to the location of where the mouse is looking (Mouse.Hit.LookVector
if I remember correctly) from the character’s rootpart. We are also going to create raycast params to exclude the player’s character. I don’t think I need to go in depth about that since you’ve said you already known about it. We can then damage the parent of the object we hit.
local gun = script.Parent --You can just get the parent of the script
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local raycastParams = RaycastParams.new()
raycastParams.FilterType = enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {character} -- Make sure it can't hit the player
gun.Activated:Connect(function()
local rayResult = workspace:Raycast(
character.HumanoidRootPart.Position,
mouse.Hit.LookVector,
raycastParams
) -- Get the result of a raycast
if rayResult and rayResult.Instance then -- If the ray has a result
if rayResult.Instance.Parent:FindFirstChildWhichIsA("Humanoid") then -- If it is a humanoid model
rayResult.Instance.Parent:FindFirstChildWhichIsA("Humanoid"):TakeDamage(10)
end
end
end)
- OK. This works, but its only client sided, meaning it only affects the player with the gun (nobody else sees anyone taking damage no matter what the player who shot sees). This is where the remote event and script from earlier come into play! We are going to fire the remote event with the direction, and handle the damage and ray on the serverside.
LOCALSCRIPT:
local gun = script.Parent --You can just get the parent of the script
local player = game.Players.LocalPlayer
local remoteEvent = script.Parent:FindFirstChildWhichIsA("RemoteEvent")
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local raycastParams = RaycastParams.new()
raycastParams.FilterType = enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {character} -- Make sure it can't hit the player
gun.Activated:Connect(function()
remoteEvent:FireServer(mouse.Hit.LookVector)
end)
SCRIPT:
local remoteEvent = script.Parent:FindFirstChildWhichIsA("RemoteEvent")
remoteEvent.OnServerEvent:Connect(function(player, direction)
local character = player.Character or player.Character:Wait() -- We have to move this here because we can't do this outside since its not local
local raycastParams = RaycastParams.new()
raycastParams.FilterType = enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {character} -- Move this, too
local rayResult = workspace:Raycast(
character.HumanoidRootPart.Position,
direction,
raycastParams
) -- Get the result of a raycast
if rayResult and rayResult.Instance then -- If the ray has a result
if rayResult.Instance.Parent:FindFirstChildWhichIsA("Humanoid") then -- If it is a humanoid model
rayResult.Instance.Parent:FindFirstChildWhichIsA("Humanoid"):TakeDamage(10)
end
end
end)
And now you can put the tool in StarterPack and start playing! Note that this is a simple rendition of what a gun could be. In fact, you could design whatever you want. Also, I wouldn’t actually use a system like this in my game because it’s not scalable, and only applies to one tool. See module scripts if you want something scalable, but it might be more complex.
This is sick, but sadly it doesn’t work for me. I’ve typed out everything, but it doesn’t do damage to the rigs I placed down.
Research, Research, Research…
Of all the talents you will gain on your way to becoming a programmer, the ability to track down and research what you intend to do is the strongest of them all. A good programmer can tell you in detail about a subject they didn’t know about a week ago… They have become outstanding at the ability to research a subject.
Yeah, sorry I was kindof just typing from memory here.
If Humanoid:Damage([damage]) doesnt work, you can also use Humanoid.Health -= [damage]. Hope this helps.