Raycast gun stops after 100 studs

I’m trying to make a raycasting gun that shoots from all kinds of distances.

The Issue that I’m having Is, that after I shoot my weapon at a distance greater than 100 studs, the gun just sort of breaks.

I have tried searching for someone with a similar Issue but I couldn’t find anything.

There are no errors, and If I shoot at a distance great than 100 studs, my muzzle flash just kind of breaks.

I don’t know at all what the Issue Is, so here Is my script

script.Parent.RayCastEvent.OnServerEvent:Connect(function(Player, FromP, ToP)
	local RayCast = Ray.new(FromP, (ToP-FromP).unit * 100)
	local Part, Position, Normal = game.Workspace:FindPartOnRay(RayCast, Player.Character, false, true)
	local shootClone = script.Parent.Base.ShootSound:Clone()
	shootClone.Parent = script.Parent.Hole
	shootClone:Play()
	script.Parent.Hole.MuzzleAttach.FlashGui.Enabled = true
	script.Parent.Hole.FlashLight.Enabled = true
	script.Parent.PlayAnimShoot:Fire()
	script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
	if Part then
		local Hole = Instance.new("Part", Part.Parent)
		local Humanoid = Part.Parent:FindFirstChild("Humanoid") or Part.Parent.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			if Humanoid.Health > 0 then
				if game.Players:FindFirstChild(Humanoid.Parent.Name) then
					local enemy = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
					if Player.TeamColor ~= enemy.TeamColor then
						Humanoid:TakeDamage(20)
						Player.leaderstats.Damage.Value = Player.leaderstats.Damage.Value + 20
						local HitSound = script.HitSound:Clone()
						HitSound.Parent = Hole
						HitSound:Play()
						if Part.Name == "Head" then
							Humanoid:TakeDamage(20)
							Player.leaderstats.Damage.Value = Player.leaderstats.Damage.Value + 20
						end
					end
				else
					Humanoid:TakeDamage(20)
					Player.leaderstats.Damage.Value = Player.leaderstats.Damage.Value + 20
					local HitSound = script.HitSound:Clone()
					HitSound.Parent = Hole
					HitSound:Play()
					if Part.Name == "Head" then
						Humanoid:TakeDamage(20)
						Player.leaderstats.Damage.Value = Player.leaderstats.Damage.Value + 20
					end
				end
			end
		else
			local NonHitSound = script.NonHitSound:Clone()
			NonHitSound.Parent = Hole
			NonHitSound:Play()
		end
		print((ToP-FromP).Magnitude)
		if (ToP-FromP).Magnitude > 30 then
			local Bullet = Instance.new("Part", game.Workspace)
			Bullet.CanCollide = false
			Bullet.Anchored = true
			Bullet.BrickColor = BrickColor.new("Cool yellow")
			Bullet.Material = Enum.Material.Neon
			
			local Dist = (ToP-FromP).magnitude
			local TweenService = game:GetService("TweenService")
			local TweenInformation = TweenInfo.new(0.07)
			Bullet.CFrame = CFrame.new(FromP, Position)
			local BulletMove = TweenService:Create(Bullet, TweenInformation, {Position = ToP})
			BulletMove:Play()
			Bullet.Size = Vector3.new(0.1, 0.1, 40)
			Bullet.Transparency = 0.8
			Bullet.Name = "Bullet"
			
			BulletMove.Completed:Connect(function()
				game.Debris:AddItem(Bullet, 0)
			end)
		end
		Hole.Size = Vector3.new(0.05, 0.05, 0.05)
		local debris = script.Parent.Base.Debris:Clone()
		debris.Parent = Hole
		debris.Enabled = true
		Hole.Name = "BulletHole"
		Hole.Transparency = 0
		Hole.Anchored = false
		Hole.CanCollide = false
		Hole.Transparency = 1
		Hole.Position = ToP
		Hole.CFrame = CFrame.new(Hole.Position, Hole.Position+Normal)
		
		local Weld = Instance.new("Weld")
		Weld.Part0,Weld.Part1 = Part, Hole
		Weld.C0 = Part.CFrame:Inverse()
		Weld.C1 = Hole.CFrame:Inverse()
		Weld.Parent = Hole
		game.Debris:AddItem(Hole, 0.4)
		wait(0.1)
		script.Parent.Hole.MuzzleAttach.FlashGui.Enabled = false
		script.Parent.Hole.FlashLight.Enabled = false
	end
end)

I also have a local script

db = true
script.Parent.Equipped:Connect(function(mouse)
	mouse.Icon = "rbxassetid://5198477188"
	script.Parent.Activated:Connect(function()
		script.Parent.RapidFire.Value = true
		while wait() do
			if script.Parent.CanShoot.Value == true then
				if script.Parent.RapidFire.Value == true then
					if script.Parent.Cocked.Value == true then
						if pcall(function() _=mouse['Hit']; end) then
							if script.Parent.Ammo.Value > 0 then
								if db == true then
									db = false
									script.Parent.RayCastEvent:FireServer(script.Parent.Hole.Position, mouse.Hit.Position)
									wait(0.15)
									db = true
								end
							elseif script.Parent.Ammo.Value == 0 then
								if db == true then
									db = false
									script.Parent.EmptyGun:FireServer()
									wait(0.25)
									db = true
								end
							end
						end
					end
				end
			end
		end
	end)
end)
local RayCast = Ray.new(FromP, (ToP-FromP).unit * 100)

The second argument being passed to the Ray constructor is the one that defines the ray’s length direction. This code right here tells it to be 100 studs long.

Change the 100 to however long you need it to be, i.e. the range of the gun in studs.

Edit: Correction - the second argument passed defines the direction, but the Ray’s length is based on the Vector3 that’s passed in for it. If the Vector3 that defines the direction is 100 studs long, then the Ray will be 100 studs long.

3 Likes

Move the two lines that hide the muzzle and the flash outside of the if Part then condition.

The issue will persist regardless of your ray length if you don’t hit a part, so although the other answer is right regarding why it’s 100 studs, it doesn’t solve the actual problem.

Essentially the last 6 lines should be in this order:

		game.Debris:AddItem(Hole, 0.4)
	end
    wait(0.1)
    script.Parent.Hole.MuzzleAttach.FlashGui.Enabled = false
	script.Parent.Hole.FlashLight.Enabled = false
end)
2 Likes