Raycast isnt working properly

I made a gun that fires raycast when it shoots, but the damaging part of it doesnt work sometimes. Its like an on and off thing. Here’s a video:
Untitled video - Made with Clipchamp
Here’s the Server Script located in ServerScriptService:

local RANGE = 99999999999999
local DAMAGE = 80

game.ReplicatedStorage.BubbleGun.OnServerEvent:Connect(function(Player, TargetLocation, Barrel)
	if Player.Character == nil then -- Make sure their character exists.
		return
	end
	

	
	local Beam = Instance.new("Part", workspace)
	Beam.BrickColor = BrickColor.new("Light pink")
	Beam.FormFactor = "Custom"
	Beam.CanCollide = false
	Beam.Transparency = 0
	Beam.Material = "Neon"
	Beam.Anchored = true
	
	local Sound = Barrel.Sound:Clone()
	Sound.Name = "Sound1"
	Sound.Parent = Barrel
	Sound:Play()
	
	-- Math.
	local Distance = (Barrel.Position - TargetLocation).magnitude
	Beam.Size = Vector3.new(.4, .4, Distance)
	Beam.CFrame = CFrame.new(Barrel.Position, TargetLocation) * CFrame.new(0, 0, -Distance/2)
	
	game.Debris:AddItem(Beam, 0.05) -- Destroy.
	
	-- Raycasting [Shoot a ray, get wherever it hit]
	local NewRay = RaycastParams.new()
	local RayDirection = (TargetLocation - Barrel.Position) * RANGE -- how far you want the ray to travel before stopping.
	
	NewRay.FilterDescendantsInstances = {Player.Character}
	
	local Result = workspace:Raycast(Barrel.Position, RayDirection, NewRay)
	
	if Result then -- IF we got a result back.
		if Result.Instance then
			-- It actually hit something.
			
			if Result.Instance.Parent:FindFirstChild("Humanoid") then
				Result.Instance.Parent.Humanoid.Health -= DAMAGE
			end
		end
	end
end)

Not 100% sure, however I can imagine that the issue is because Result.Instance.Parent isn’t necessarily the character. It might be that Result.Instance is some sort of object inside of an accessory of an player (as an example now); then Result.Instance.Parent would be the accessory, which obviously doesn’t have an humanoid. You’d have to make sure that Instance.Parent is the character. Maybe :FindFirstAncestorOfClass() might be handy. You could check what the next model above the Result.Instance is, and check if that model has a humanoid inside it.

image
Example: If Handle gets hit here, then the Parent is the PlaidWrapHat, which doesn’t contain a Humanoid => no damage is dealt.


:FindFirstAncestorOfClass() might not be the cleanest method (if this is even the issue), because if the first ancestor of the part which is a model is not the character (which might be the case if there is another model inside of the character), then this would not register, though I can’t think of any better method right now.

Maybe use a combo of :FindFirstAncestorOfClass() and :FindFirstChild() to check if there is a humanoid in the model.

Thx for the suggestion but how would that look in script? I’m not sure how I would type that out to make it work.

Lets see.

if Result then -- Check if we got a result.
		if Result.Instance then -- Check if we hit something
            local model = script.Parent:FindFirstAncestorOfClass("Model") -- Go up the hierarchy until we find an instance that is a model.
			if model:FindFirstChild("Humanoid") then -- Check if that model parents a humanoid, if it does, we know it's a player.
				model.Humanoid.Health -= DAMAGE
			end
		end
	end

That should work but it may not work the best if there are other models stored inside the players character. It works in most cases though.

1 Like

It worked! tysm for helping me out! :slightly_smiling_face:

No problem :smiley:
Try to avoid adding models to the players character or it might break, but I’m sure you can find a way around it!

1 Like

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