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

Raycast is damaging me as well?

Hello,

just running a simple Raycast script, etc...except...this is the weird part...so I can fire forwards and everything is fine but when I turn the camera to the rear and fire, it damages me as well.

No errors pop up or anything and its bizarre because only this particular model (my ship) is doing it. Same scripts are used for my tanks as well and does not suffer this same effect.

--Server Script

  	local user = player 

    local ray = Ray.new(BarrelPos.CFrame.p, (target - BarrelPos.CFrame.p).unit * 300)
    local hit, position = game.Workspace:FindPartOnRay(ray, user)

    local distance = (position - BarrelPos.CFrame.p).magnitude
    local RayPart = Instance.new("Part", user)
    RayPart.Name = "BulletPath"
    RayPart.BrickColor = RayColor
    RayPart.Transparency = 0
	RayPart.Material = "Neon"
    RayPart.Anchored = true
  	RayPart.CanCollide = false
    RayPart.TopSurface = Enum.SurfaceType.Smooth
    RayPart.BottomSurface = Enum.SurfaceType.Smooth
    RayPart.formFactor = Enum.FormFactor.Custom
    RayPart.Size = Vector3.new(0.5, 0.5, 7)
    RayPart.CFrame = CFrame.new(position, BarrelPos.CFrame.p) * CFrame.new(0, 0, -distance/1.16)
	local MeshPart = Instance.new("SpecialMesh", RayPart)
	MeshPart.MeshType = "Sphere"
	local goal = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.In ,0 , false, 0.1)
	local partproperties = {CFrame = CFrame.new( target, BarrelPos.Position), Transparency = math.random (0.1,1)}
	local Tween = game:GetService("TweenService"):Create(RayPart, goal, partproperties)
	Tween:Play()
    game.Debris:AddItem(RayPart, 0.1)
	
	if target then
		local splash = Instance.new("Part", game.Workspace)
		splash.Name = "BulletHole"
		splash.Anchored = true
		splash.Transparency = 1
		splash.CanCollide = false
		splash.Size = Vector3.new(0.001,0.001,0.001)
		splash.CFrame = CFrame.new(target)	
		local TankDust = script.TankDustEmitter:Clone()
		TankDust.Enabled = true
		TankDust.Parent = splash
		Debris:AddItem(splash, 1.25)
	
	local Damage = math.random(25,100)	
	local TargetObj = hit and hit.Parent.Parent:FindFirstChild("Config")
	if TargetObj and TargetObj:FindFirstChild("Hull") then
		TargetObj.Hull.Value = TargetObj.Hull.Value - Damage
		print("Hit Vehicle for "..Damage)
	end
--Client Script

FireAccuracy = Vector3.new(math.random(-0.7, 0.7), math.random(-0.7, 0.7), math.random(-0.7, 0.7))

local function computeDirection(vec)
	local lenSquared = vec.magnitude * vec.magnitude
	local invSqrt = 1 / math.sqrt(lenSquared)
	return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end

local function TankRnd(v)
local Mouse = Player:GetMouse()
	if Active then
		if TankGun then
			if ((Vehicleseat.Parent.Hull.TankBarrelFlash.Position - Mouse.Hit.p).magnitude) < Range then
				BarrelFlashEvent:FireServer(tool, Vehicleseat.Parent.Hull.TankBarrelFlash, 0.5)
				tool.Sounds:FindFirstChild("TankFire".. math.random(3)):Play()
				TankGunEvent:FireServer(Mouse.Hit.p + FireAccuracy, Vehicleseat.Parent.Hull.TankBarrelFlash, BrickColor.new("Bright yellow"), BrickColor.new("Cool yellow"))
			end
		end
	end	
end	

1 answer

It's because you are ignoring the "player" in the ray, raycasts don't ignore players, they can ignore characters, so put this instead

local user = player
local character = player:WaitForChild("Character") -- The character

local ray = Ray.new(BarrelPos.CFrame.p, (target - BarrelPos.CFrame.p).unit * 300) -- The same
local hit, position = game.Workspace:FindPartOnRay(ray, character)

2 comments

0

Sorry, just errors out with an infinite yield message.

0

local character = player.CharacterAdded:Wait()

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