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)
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
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?
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.
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.