Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does the attacking Player taking damage from their own weapon/gun?

Hello there! I was wondering why your own gun does damage to you instead of your target, like a dummy for example. This is really something that I don't know how to script, so I would appreciate it if I got some help; Thanks! More Info below!

-- Variables
local TweenService = game:GetService("TweenService")
local Tweeninfo = TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
-- Scripting in Action
script.Parent.ShootEvent.OnServerEvent:Connect(function(player, caster, position)
	local rayOrigin = caster.Position
	local rayDirection = position
	
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {player.Character}
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local rayResult = game.Workspace:Raycast(rayOrigin, (position - script.Parent.Handle.Position) * 200, rayParams)
	
	local hittedPart = rayResult.Instance
	
	local bullet = Instance.new("Part", game.Workspace)
	bullet.Size = Vector3.new(0.25, 0.25, 0.25)
	bullet.Shape = Enum.PartType.Cylinder
	bullet.Position = script.Parent.PistolModel.Flash.Position
	bullet.BrickColor = BrickColor.new("Gold")
	
	local TweenTo = {CFrame = CFrame.new(position)}
	
	local TweenBullet = TweenService:Create(bullet, Tweeninfo, TweenTo)
	
	TweenBullet:Play()
	
	if hittedPart.Parent:IsA("Model") then
		bullet.Touched:Connect(function(hit)
			local charcter = hit.Parent
			
			local humanoid = charcter:FindFirstChild("Humanoid")
			
			if humanoid then
				bullet:Destroy()
				if hittedPart.Name == "Head" then
					humanoid:TakeDamage(25)
					print("Headshot Acquired!")
				else
					humanoid:TakeDamage(16)
					print("Shot!")
				end
			else
				wait(5)
				bullet:Destroy()
			end
		end)
	end
	
end)

1 comment

0

I don't really use tweenservice but I made a gun thats easy to script, It may not be based off the problem you have having but can I show It to you?

2 answers

✓ Accepted answer

The problem it's that inside the Raycast params, you should put instead of the model, the childrens, or atleast the ones that can get hitted by the ray using :GetChildren(). I'd recommend making a table and putting inside all the BaseParts and Accessories of the player. Also, where does the variable position come from?

Edit: I marked your answer correct, I've used another source of information to help out with my scripting problem, a DevForum post, but anyway thanks for trying to help! I really appreciate it!

This discussion is preserved as it was. New answers, comments, and votes are closed.