Raycast detect a humanoid and reduce its damage

Ah yes, this should surefire work, although would this instead shoot from the person itself instead of the actual gun? Or am I missing something…

Like what about:

local rayDirection = (Mouse - Tool.Handle.Position).unit * 100


This is quite strange. If I shoot low near the ground, then it works. Seems like the raycast goes a bit higher than where the mouse is pointed.

can you post the whole code so we can see what is actually in it?

Fire the ray from the humanoidrootpart to the destination. fire the effects from the tool’s handle position to the destination if possible.

As for having a holdingtool variable, if it has anything to do with a Context (like reloading) then i reccommend checking out ContextActionService

This is the LocalScript that gets the mouse:

local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local animationFolder = replicatedStorage.ClientAnimations
local smallGunAimShot = animator:LoadAnimation(animationFolder.SmallGunAimShot)
local smallGunShot = animator:LoadAnimation(animationFolder.SmallGunShot)
local reload = animator:LoadAnimation(animationFolder.Reload)

local mouse = player:GetMouse()
local mouse2 = false

local tool = script.Parent
local cooldown = true
local holdingTool = false
local gunEmpty = false

local magTotal = 2
local shootAmount = 0
local cooldownTime = 1

mouse.Button2Down:Connect(function()
	mouse2 = true
end)
mouse.Button2Up:Connect(function()
	mouse2 = false
end)

tool.Equipped:Connect(function()
	holdingTool = true
end)
tool.Unequipped:Connect(function()
	holdingTool = false
end)

local function fireGun()
	cooldown = false
	shootAmount = shootAmount + 1

	humanoid.WalkSpeed = 4

	local animation = if mouse2 then smallGunAimShot else smallGunShot
	animation:Play()

	replicatedStorage.ToolRemoteEvents.DB:FireServer(mouse.Hit.Position, tool)

	local camera = game.Workspace.CurrentCamera
	camera.CFrame = camera.CFrame * CFrame.Angles(0.02, 0 ,0)

	if shootAmount >= magTotal then
		gunEmpty = true
	end
	task.wait(cooldownTime)

	cooldown = true
	humanoid.WalkSpeed = 16
end

local function requestReload()
	if not (holdingTool or gunEmpty) then
		return
	end

	if shootAmount < magTotal then
		return
	end

	shootAmount = 0
	cooldown = false

	replicatedStorage.ReloadWeapon:FireServer()
	humanoid.WalkSpeed = 5
	reload:Play()
	task.wait(2)

	gunEmpty = false
	cooldown = true
end

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R or input.UserInputType == Enum.UserInputType.Touch then
		requestReload()
	end
end)

tool.Activated:Connect(function()
	if not cooldown then
		return
	end

	if shootAmount >= magTotal then
		if gunEmpty then
			replicatedStorage.ToolRemoteEvents.DB:FireServer(mouse.Hit.Position, tool)
			return
		end
	end

	if character:FindFirstChild("Reload").Value then
		return
	end

	fireGun()
end)

And this is the ServerScript that does the damage:

local serverScriptService = game:GetService("ServerScriptService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local dbBeam = serverScriptService:WaitForChild("DB_Beam")

replicatedStorage.ToolRemoteEvents.DB.OnServerEvent:Connect(function(Player, Mouse, Tool)
	local character = Player.Character

	if Tool.Ammo.Value >= 1 then
		local GUI = Player.PlayerGui:WaitForChild("Weapons")["Double-Barrel SG"]
		local GoingToShootBullet = GUI.MainFrame:FindFirstChild("BulletImage")

		if GoingToShootBullet then
			GoingToShootBullet.ImageTransparency = 0.6
			GoingToShootBullet.Name = "UsedBullet"

			if character:WaitForChild("Reload").Value == false then
				character:WaitForChild("Reload").Value = true
				Tool.Barrel.ShootUI.Shoot.Visible = true
				Tool.Handle.ShootLight.Enabled = true
				Tool.Barrel.ShootUI.Shoot.ImageTransparency = 0.6

				local Shoot = Instance.new("Sound")
				Shoot.Parent = Tool.Handle
				Shoot.SoundId = "rbxassetid://3855292863"
				Shoot:Play()

				dbBeam:Fire(Mouse, Tool)
				dbBeam:Fire(Mouse, Tool)
				dbBeam:Fire(Mouse, Tool)
				dbBeam:Fire(Mouse, Tool)
				dbBeam:Fire(Mouse, Tool)
				dbBeam:Fire(Mouse, Tool)
				dbBeam:Fire(Mouse, Tool)

				print("mouse position: " .. tostring(Mouse)) 

				local rayDirection = (Mouse - Tool.Handle.Position).unit * 100
				print("ray direction: " .. tostring(rayDirection))

				local rayDesti = rayDirection
				print("ray desti: " .. tostring(rayDesti))

				local Params = RaycastParams.new()
				Params.FilterDescendantsInstances = {character}
				Params.FilterType = Enum.RaycastFilterType.Exclude

				local raycast = workspace:Raycast(Tool.Handle.Position, rayDesti, Params) 
				
				local beam = Instance.new("Beam")
				beam.Parent = workspace
				beam.Attachment0 = Instance.new("Attachment")
				beam.Attachment0.Parent = Tool.Handle
				beam.Attachment0.Position = Vector3.new(0, 0, 0)
				beam.Attachment1 = Instance.new("Attachment")
				beam.Attachment1.Parent = workspace.SpawnLocation
				beam.Attachment1.Position = rayDesti
				
				if raycast then
					print("raycast hit something")
					if raycast.Instance.Parent:FindFirstChild("Humanoid") then
						print("humanoid found") -- Print if Humanoid found
						local humanoid = raycast.Instance.Parent:FindFirstChild("Humanoid")
						humanoid.Health -= 20
					else
						print("humanoid not found")
					end
				else
					print("raycast did not hit anything")
				end
				
				wait(0.2)
				Tool.Barrel.ShootUI.Shoot.Visible = false
				character:WaitForChild("Reload").Value = false
				Tool.Handle.ShootLight.Enabled = false
				wait(1)

				Shoot:Destroy()
			end
		else
			Tool.Handle.NoAmmo:Play()
		end
	end
end)

And this is the other ServerScript that does the beams that come out the gun when it shoots:

local serverScriptService = game:GetService("ServerScriptService")

serverScriptService.DB_Beam.Event:Connect(function(Mouse, Tool)
	local EndPoint = Instance.new("Part")
	local StartPoint = Instance.new("Part")

	EndPoint.Transparency = 1
	EndPoint.Parent = game.Workspace
	
	local Distance = (Mouse - StartPoint.Position).Magnitude
	
	if Distance < 35 then
		EndPoint.Position = Mouse + Vector3.new(math.random(0, 1), math.random(0, 0.5), math.random(0, 1))	
	else
		EndPoint.Position = Mouse + Vector3.new(math.random(1, 2), math.random(0, 1), math.random(1, 2))
	end
	
	EndPoint.Size = Vector3.new(0.5, 0.5, 0.5)
	EndPoint.Anchored = true
	EndPoint.CanCollide = false

	StartPoint.Transparency = 1
	StartPoint.Parent = game.Workspace
	StartPoint.Position = Tool.Barrel.Position
	StartPoint.Size = Vector3.new(0.5, 0.5, 0.5)
	StartPoint.Anchored = true
	StartPoint.CanCollide = false

	local Beam = Instance.new("Beam")
	local Attachment_0 = Instance.new("Attachment")
	local Attachment_1 = Instance.new("Attachment")

	Beam.Parent = StartPoint
	Attachment_0.Parent = StartPoint
	Attachment_1.Parent = EndPoint
	
	Beam.Color = ColorSequence.new({ -- a color sequence shifting from white to blue
		ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 170, 0)),
		ColorSequenceKeypoint.new(1, Color3.fromRGB(255, 255, 127)),
	})
	
	Beam.LightEmission = 1
	Beam.Attachment1 = Attachment_1
	Beam.Attachment0 = Attachment_0
	Beam.Width0 = 0.002
	Beam.Width1 = 0.09
	Beam.FaceCamera = true
	task.wait(0.1)
	
	Beam:Destroy()	
	Attachment_0:Destroy()
	Attachment_1:Destroy()
	StartPoint:Destroy()
	EndPoint:Destroy()	
end)

add Tool to the param filter

Params.FilterDescendantsInstance = {character, Tool}
1 Like
	if raycastResult then
		if raycastResult.Instance then
			if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
				if game.Players:GetPlayerFromCharacter(t) then
					t.Parent.Humanoid.Health -= 1 -- DAMAGE
                    -- You need to change the  "t" just to the raycast hit and then check if it has a humanoid
				end
			end
		end
	else
		warn("No raycast result!")
	end


Wow… that seems to have done it… huh. Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.