How to add Raycast?

Hello, I’m trying to make raycast (bullets) for my guns in roblox studio. But I’m not sure where to start.
I want to try and Make Pistol, Automatic, and Shotgun Raycast. It also should be able to do damage the player.

Here is the Server:

local db = false
script.Parent.DealDamage.OnServerEvent:Connect(function(player, Target, Damage)
	print("Damage Taken.")
	Target.Humanoid:TakeDamage(Damage)
	if Target.Humanoid.Health <= 0 then
		if db == false then
			db= true
			player.leaderstats.Points.Value = player.leaderstats.Points.Value + 15
			player.leaderstats.Kills.Value = player.leaderstats.Kills.Value + 1
			task.wait(3)
			db = false
		end
	end
end)

Client:

local tool = script.Parent
local maxammo = 13
local CoolDown = 0.23
local ammo = maxammo
local reloading = false
local plr = game.Players.LocalPlayer
local plrgui = plr:WaitForChild("PlayerGui")
local text = plrgui:WaitForChild("Ammo"):FindFirstChild("AmmoCount")
local Mouse = plr:GetMouse()

script.Parent.Equipped:Connect(function()
	
	plr.CameraMode = Enum.CameraMode.LockFirstPerson

	plrgui.Ammo.Enabled = true
	text.DisableScript.Disabled = true
	plrgui.Shop.Enabled = false
	local function reload()
		reloading = true
		wait(1)
		script.Parent.reload:Play()
		ammo = maxammo
		reloading = false
	end

	script.Parent.Activated:Connect(function()

		local TargetPos = plr.Character:FindFirstChildOfClass("Humanoid").TargetPoint
		if ammo > 0 and not reloading then
			ammo = ammo - 1
			script.Parent.gunshot:Play()
			wait(CoolDown)
			local targetHumanoid = Mouse.Target.Parent:FindFirstChild("Humanoid")
			if targetHumanoid then
				print("Fire.")
				script.Parent.DealDamage:FireServer(Mouse.Target.Parent, math.random(15,30))
				local player = game.Players.LocalPlayer
			else
				print("Shot wall, not player.")
			end
		elseif reloading == false then
			reload()
			script.Parent.gunshot:Stop()
		end
		
		while wait() do
			text.Text = (ammo).." / "..tostring(maxammo)
		end
	end)

	local input = game:GetService("UserInputService")
	input.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxammo then
			reload()
		end
	end)

	local input = game:GetService("UserInputService")
	input.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.ButtonY and reloading == false and ammo ~= maxammo then
			reload()
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	plrgui.Ammo.Enabled = false
	text.DisableScript.Disabled = false
	plrgui.Shop.Enabled = true
	plr.CameraMode = Enum.CameraMode.Classic
end)

I’m not asking for someone to script for me, and just find something for me to script on my own.

Thanks, papahetfan

Please take your time into reading this.

The basics

Raycasting is done with the following line of code:
local result = workspace:Raycast(origin, direction, [parameters])
origin is a Vector3. Pretty sure it’s self explanatory
direction is a Vector3 as well.
[parameters] is a RaycastParams class. It’s optional.
result is a RaycastResult class. It can be nil!

RaycastResult

Contains 5 fields, those are:

  • Instance - The object that was hit.
  • Position - Where the ray landed.
  • Normal - The hit normal of the object (very useful when making bullet holes).
  • Distance - Distance between origin and the hit position.
  • Material - Do I really need to explain?

Note: this can be nil! Make sure you check that a RaycastResult even exists.

Firing a ray from a start position to an end position

This is made really simple by unit vectors.
If you didn’t know, those are vectors that have a range of 0 to 1.
This is how to calculate a direction from startPos to endPos
local direction = (endPos - startPos).Unit * (max length)
The multiplication is done to extend the vector (remember, it returns a unit vector)
Pretty simple, right?

RaycastParams

Those are parameters for the raycast. You need to create them with RaycastParams.new()
After that, you can access 4 different fields.

  • FilterType - The type of the filter. (either Enum.RaycastFilterType.Blacklist or Whitelist)
  • FilterDescendantsInstances - A table of all the instances that you want to blacklist OR whitelist.
  • IgnoreWater - If false, the raycast will get stopped by terrain water.
  • CollisionGroup - Collision group that the raycast will use. Use this if you have invisible parts that you can go through.

Let me know if you didn’t understand anything and need an example.

2 Likes

It is recommended to do the damaging value inside the server, I’ve seen you’re sending the damage values from client which exploiter can just change it to OP dmg. So Instead sending from client to server, try to do the dmg random inside the server without doing damage parameter.