So I made this rifle, nothing is wrong with it but my only problem is the tracer. Here’s my script:
local equipped = false
script.Parent.Equipped:Connect(function()
equipped = true
print(equipped)
end)
script.Parent.Unequipped:Connect(function()
equipped = false
print(equipped)
end)
local fired = false
local fireTime = 0
local eventTime = 0
local fireRate = 1
local function countCheck()
if fireTime-eventTime >= fireRate or fireRate-(fireTime-eventTime) <= 1/30 then
return true
end
end
script.Parent.MouseClicked.OnServerEvent:Connect(function(player, mousePos)
if not player.Character == script.Parent.Parent then return end
end)
local function newBullet()
local bullet = Instance.new("Part")
bullet.Name = "Bullet"
bullet.Parent = workspace.BulletStorage
bullet.Size = Vector3.new(0.3, 0.125, 0.125)
bullet.Transparency = 0
bullet.Position = script.Parent.Rifle.ShootingPoint.Position
bullet.Color = Color3.new(1, 1, 0)
bullet.Material = Enum.Material.Neon
bullet.CanCollide = false
bullet:SetNetworkOwner(nil)
return bullet
end
local function applyPhysics(bullet, mousePos)
local muzzleVelocity = 300
local direction = CFrame.new(bullet.Position, mousePos)
local horizontalSpread = 4
local verticalSpread = 4
horizontalSpread = math.rad(horizontalSpread)
verticalSpread = math.rad(verticalSpread)
direction = direction*CFrame.Angles(horizontalSpread, verticalSpread, 0)
direction = direction.LookVector
local mass = bullet:GetMass()
bullet:ApplyImpulse(direction*mass*muzzleVelocity)
end
local function fireAmmo(player, mousePos)
local bullet = newBullet()
applyPhysics(bullet, mousePos)
bullet.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("Humanoid"):TakeDamage(15)
end
end)
local RNS = game:GetService("RunService")
local lastPos = bullet.Position
local rayFilter = RaycastParams.new()
rayFilter.FilterDescendantsInstances = {player.Character, workspace.BulletStorage}
rayFilter.FilterType = Enum.RaycastFilterType.Blacklist
local range = 300
local tracer = workspace.Visual:GetChildren()[1]
if tracer then
tracer.Parent = workspace.BulletStorage
else
tracer = Instance.new("Part", workspace.BulletStorage)
tracer.Anchored = true
tracer.CanCollide = false
tracer.Position = bullet.Position + Vector3.new(0, 1e7, 0)
end
tracer.BrickColor = BrickColor.new("Gold")
tracer.Material = Enum.Material.Neon
tracer.Position = bullet.Position*Vector3.new(0, 2, 0)
while range > 0 do
task.wait()
local rayLength = (lastPos-bullet.Position).Magnitude
range = range - rayLength
lastPos = bullet.Position
local rayPos = bullet.Position
local rayDirection = CFrame.new(lastPos, rayPos).LookVector
tracer.Size = Vector3.new(.1, .1, (lastPos-rayPos).Magnitude)
tracer.CFrame = CFrame.new(rayPos*lastPos)*CFrame.new(0, 0, -(lastPos-rayPos).Magnitude/2)
local ray = workspace:Raycast(lastPos, rayDirection*rayLength, rayFilter)
if ray then
bullet:Destroy()
local hit = ray.Instance
print(hit:GetFullName())
local model = hit:FindFirstAncestorOfClass("Model")
if model then
local humanoid = model:FindFirstChildWhichIsA("Humanoid") or nil
if humanoid then
if hit.Head then
humanoid:TakeDamage(45)
else
humanoid:TakeDamage(15)
end
return true
end
end
task.spawn(function()
task.wait()
task.wait()
tracer.Position = Vector3.new(0, 1e7, 0)
tracer.Parent = workspace.Visual
end)
break
end
if bullet.Parent == nil then
bullet:Destroy()
break
end
lastPos = bullet.Position
end
task.spawn(function()
task.wait()
task.wait()
tracer.Position = Vector3.new(0, 1e7, 0)
tracer.Parent = workspace.Visual
end)
if bullet then bullet:Destroy() end
end
script.Parent.MouseClicked.OnServerEvent:Connect(fireAmmo)
And the local script:
local player = game.Players.LocalPlayer
local screen = Instance.new("ScreenGui")
screen.Name = "SgreenGUI"
screen.Parent = player.PlayerGui
screen.Enabled = false
local GUI = Instance.new("ImageLabel")
GUI.Name = "GUI"
GUI.Parent = screen
GUI.Visible = true
GUI.AnchorPoint = Vector2.new(0.5, 0.5)
GUI.Position = UDim2.new(0.5, 0, 0.5, 0)
GUI.Size = UDim2.new(0, 100, 0, 100)
GUI.Image = "rbxassetid://9551517714"
GUI.BackgroundTransparency = 1
local click = script.Parent["Gun Click"]
local gunshot = script.Parent.Gunshot
local mouse = player:GetMouse()
local equipped = false
local firing = false
script.Parent.Equipped:Connect(function()
equipped = true
screen.Enabled = true
click:Play()
print(equipped)
end)
script.Parent.Unequipped:Connect(function()
equipped = false
screen.Enabled = false
print(equipped)
end)
mouse.Button1Down:Connect(function()
if equipped and not firing then
firing = true
script.Parent["Gun Click"]:Play()
while firing == true do
mouse.Button1Up:Connect(function()
firing = false
end)
mouse.TargetFilter = workspace.BulletStorage
script.Parent.MouseClicked:FireServer(mouse.Hit.Position)
print("Pew!")
gunshot:Play()
task.wait(0.15)
end
end
end)
There’s no errors. Any tips on how to fix this issue?