Hi there!
I created a simple gun script where both a Raycast and bullet trail will fire according to the LookVector of the gun’s barrel. My goal is to know create bullet HIT effects, which requires me to get the position of the Raycast Results, I tried using RaycastResults.Position…but it doesnt work. Any solutions?
tool.Activated:Connect(function()
if debounce == false and ammo > 0 and Reloading == false then
debounce = true
game.Workspace.Sound.Gunshot:Play()
ammo -=1
--=======Bullet Effects=======================
local Point = tool.Handle.Point
local PointLookVector = Point.CFrame.LookVector
local Point = script.Parent.Handle.Point
local function BulletRay()
local midpoint = Point.Position + PointLookVector * 500/2
local part = Instance.new("Part")
part.Parent = workspace
part.Anchored = true
part.CFrame = CFrame.new(midpoint, Point.Position)
part.Size = Vector3.new(0.1, 0.1, (PointLookVector * 500).magnitude)
part.Material = Enum.Material.Neon
part.BrickColor = BrickColor.new("Gold")
part.CanCollide = false
wait(0.05)
part:Destroy()
return part
end
BulletRay()
--=======Damage RemoteEvent=======================
local RaycastParams = RaycastParams.new()
RaycastParams.FilterDescendantsInstances = {tool.Parent}
RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
local Point = tool.Handle.Point
local doDmgRE = tool:WaitForChild("DoDamage")
local RaycastResult = workspace:Raycast(Point.Position, PointLookVector * 100, RaycastParams)
if RaycastResult then
local RaycastInstance = RaycastResult.Instance
local Findmodel = RaycastInstance:FindFirstAncestorOfClass("Model")
if Findmodel then
local FindPlayerHumanoid = Findmodel
if FindPlayerHumanoid:FindFirstChild("Humanoid") then
local targetPlayer = FindPlayerHumanoid
local dmgHumanoid = 100
doDmgRE:FireServer(targetPlayer, dmgHumanoid)
end
end
else end
task.wait(0.01)
Player.PlayerGui["Ammo GUI"].Frame.TextLabel.Text = "Ammo: "..ammo.."/"..maxammo
debounce = false
elseif ammo <= 0 then
return
end
end)
yes I know but when I tried to replace all PointLookVector with RaycastResult.Position…it just offsets the ray completely. PointLookVector is basically RaycastResult.
local function BulletRay()
local midpoint = Point.Position + RaycastResult.Position * 500/2
local part = Instance.new("Part")
part.Parent = workspace
part.Anchored = true
part.CFrame = CFrame.new(midpoint, Point.Position)
part.Size = Vector3.new(0.1, 0.1, (RaycastResult.Position * 500).magnitude)
part.Material = Enum.Material.Neon
part.BrickColor = BrickColor.new("Gold")
part.CanCollide = false
wait(0.05)
part:Destroy()
return part
end
BulletRay()
local Debris = game:GetService("Debris")
-- Tracer settings
local TracerSize = 0.25
local TracerColour = BrickColor.new("Gold")
local TracerMaterial = Enum.Material.Neon
function BulletRay()
local RayDistance = RayResult.Distance
local RayPosition = RayResult.Position
-- Create new part, set size and CFrame
local Tracer = Instance.new("Part")
Tracer.Size = Vector3.new(TracerSize, TracerSize, RayDistance)
-- The tracer is positioned to start and end at the origin of the raycast and where it hit.
-- The second CFrame is because otherwise the middle of the tracer part will be somewhere we don't want
-- And you're gonna want to set 'RayOrigin' to whatever your gun muzzle is set as
Tracer.CFrame = CFrame.new(RayOrigin, RayPosition) * CFrame.new(0, 0, -RayDistance / 2)
Tracer.Material = TracerMaterial
Tracer.BrickColor = TracerColour
Tracer.Anchored = true
Tracer.CanCollide = false
Tracer.Parent = workspace
Debris:AddItem(Tracer, 0.05)
end
local function CreateTracer(startPosition, endPosition)
local tracer = Instance.new("Part") --put your part from workspace in here
tracer.Anchored = true
tracer.CanCollide = false
tracer.Size = Vector3.new(0.1, 0.1, (startPosition - endPosition).Magnitude)
tracer.CFrame = CFrame.new(startPosition, endPosition) * CFrame.new(0, 0, -tracer.Size.Z / 2)
tracer.Parent = workspace
-- Destroy the tracer after a certain duration
wait(0.1)
tracer:Destroy()
end
-- Inside your tool.Activated event handler
local RaycastResult = workspace:Raycast(Point.Position, PointLookVector * 100, RaycastParams)
if RaycastResult then
local hitPosition = RaycastResult.Position
CreateTracer(Point.Position, hitPosition)
-- Rest of your code
-- ...
end```
Tracer works but there seem to be an error whenever the ray is fired towards nothing as in neither a part or model. This is the error message: attempt to index nil with ‘Distance’
How should I attempt to fix this as the distance towards nothing will not produce a Raycast Result…
The solution for that is to calculate the position that it would be. To do this, you just need to take the ray direction, divide it by 4 and add that to the origin position.
Diving specifically by 4 is more of an arbitrary number as the higher number you divide with, the closer the position of the tracer is. Diving by 4 may put the tracer may be only 700 studs away, but dividing by 2 will place it nearly 2k studs away. Just remember you don’t want to exceed 2048 studs so I’d recommend keeping the number between 5 and 3.
-- If the RayResult returned null (meaning it hit nothing such as the skybox), replace RayPosition
if not RayResult then
-- The RayDirection variable will be whatever your variable for the ray direction is!
-- Divide the RayDirection by 4 then add the ray origin Vector3 to the calculated position
local CalculatedPosition = RayDirection / 4
RayPosition = RayOrigin + CalculatedPosition
end
Updated code from my last reply:
local Debris = game:GetService("Debris")
-- Tracer settings
local TracerSize = 0.25
local TracerColour = BrickColor.new("Gold")
local TracerMaterial = Enum.Material.Neon
function BulletRay()
local RayDistance = RayResult.Distance
local RayPosition = RayResult.Position
-- If the RayResult returned null (meaning it hit nothing such as the skybox), replace RayPosition
if not RayResult then
-- The RayDirection variable will be whatever your variable for the ray direction is!
-- Divide the RayDirection by 4 then add the ray origin Vector3 to the calculated position
local CalculatedPosition = RayDirection / 4
RayPosition = RayOrigin + CalculatedPosition
end
-- Create new part, set size and CFrame
local Tracer = Instance.new("Part")
Tracer.Size = Vector3.new(TracerSize, TracerSize, RayDistance)
-- The tracer is positioned to start and end at the origin of the raycast and where it hit.
-- The second CFrame is because otherwise the middle of the tracer part will be somewhere we don't want
-- And you're gonna want to set 'RayOrigin' to whatever your gun muzzle is set as
Tracer.CFrame = CFrame.new(RayOrigin, RayPosition) * CFrame.new(0, 0, -RayDistance / 2)
Tracer.Material = TracerMaterial
Tracer.BrickColor = TracerColour
Tracer.Anchored = true
Tracer.CanCollide = false
Tracer.Parent = workspace
Debris:AddItem(Tracer, 0.05)
end