Bullet affect it's owner and not working at others

I created a gun and finalizing it. But something went wrong is the bullet does not affect others when got hit and affect to it’s owner

I created damage script where it clones each the bullet got out from it’s muzzle

local beam = script.Parent
local playername = script:WaitForChild("Value")
local player = game.Players:FindFirstChild(playername.Value)

beam.Touched:Connect(function(hit)
	print("Touched:", hit.Name)
	if player and hit:IsDescendantOf(player.Character) then
		print("lol")
	end
end)

Anything wrong here?

3 Likes

Is the script a local script or a server script?

Given that it was outputting to the client console, I am assuming it is a local script.
Try having the bullet be created on the server instead of the client. As far as the server is concerned (and the other players) the bullet simply doesn’t exist, hence it doesn’t kill other players

Server script actually

30 words there you go

I think the damage script is a local script, because it is outputting to the client console instead of the server console.


How is the bullet being moved? Is it via a tween, and is the bullet anchored?
Is the dummy also anchored?

.Touched doesn’t function when the 2 parts are anchored

  1. The bullet is anchored

  2. The bullet being moved with CFrame (which it loops)

What about the rig that you are trying to hit? Is it anchored?

Most of the rig’s part is not anchored except the HumanoidRootPart

As the HumanoidRootPart is anchored, and the bullet is itself anchored, the .Touched event will not fire.
If you unanchor the HumanoidRootPart, does the script function as intended?

Yeah it does now… But it didn’t print the non-humanoid parts (which is normal part)

Do you mean like the baseplate?

Yeah and the part is anchored also

Whenever both parts are anchored, you will encounter an issue with it not detecting it.
You may want to look at using raycasting in order to find what the bullet will collide with.

Why don’t you raycast from the bullet instead of using Touched?

Oh yes it works but the mouse hits first before the bullet hits

	local RayResult = workspace:Raycast(beam.CFrame.p, (MouseP - beam.CFrame.p).unit*90000, RayFilter)
		
		if RayResult then
			local HitPart = RayResult.Instance
			print("Ray hits ".. HitPart:GetFullName())
			
			beam:Destroy()
			
			if HitPart.Parent:FindFirstChild("Humanoid") then
				local Humanoid = HitPart.Parent:FindFirstChild("Humanoid")
				Humanoid:TakeDamage(Damage.Value)
			end
		end

I don’t know how to print the result when it hits the bullet than the mouse

That is unfortunately a side effect of using raycast.

Another alternative is to call :GetPartsInParts or :GetTouchingParts every time the bullet is moved, then looping through the returned table and processing it how you want.

Just raycast infront of the bullet instead.

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