Bullet doesnt appear when shooting to skybox

If anybody who know raycast will know that it can make a bullets (parts) like a prison life gun, but in prison life, you can shoot to skybox and will appear a bullets, but when i tried doing the bullets, it will go to the midpoint (which skybox are infinite) so you cannot see a bullet. Any Idea how to fix this?

code:

event.OnServerEvent:connect(function(play, mouse)
	local origin = handle.Position
	local direction = (mouse.p - handle.Position).Unit * (mouse.p - handle.Position).Magnitude
	local ray = Ray.new(origin,direction) 
	local impact = game.Lighting.Bullet:Clone()
	local midpoint = origin + direction/2
	impact.Size = Vector3.new(0.2,0.2,(mouse.p - handle.Position).Magnitude)
	impact.CFrame = CFrame.new(midpoint,mouse.p)
	--impact.Mesh.Scale = Vector3.new(1,1,(mouse.p - handle.Position).Magnitude)
	impact.Parent = game.Workspace.Bullets
	local hit,Position = workspace:FindPartOnRayWithIgnoreList(ray, {char, impact, tool, game.Workspace.Bullets})
	if hit then
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= tool.Parent.Name then
			if debounce == true then debounce = false
			if hit.Name == "Head" or hit.Name == "Face" then
			hit.Parent.Humanoid:TakeDamage(60)
			wait(0.05)
			debounce = true
			else
			hit.Parent.Humanoid:TakeDamage(30)
			wait(0.05)
			debounce = true
				end
			end
		end
	end
	script.Parent.Hole.Sound.TimePosition = 0
	script.Parent.Hole.Sound:Play()
	game:GetService("Debris"):AddItem(impact, 0.05)
3 Likes

make invisible walls or use math.clamp to limit ray direction

Sorry, i cannot find a math.clamp() in devhub so I dont know much about it. Could you give me an example?

Oh wait nvm i found it! Let me try it rq!

for you it would be

local minimalDis = 0
local maximumDis = 9999
local direction = (mouse.p - handle.Position).Unit * math.clamp((mouse.p - handle.Position).Magnitude,minimalDis,maximumDis)

forgot to mention:
i cant check it rn, but if you point your mouse at sky it mouse.p would return nil isntead of Vector3

EDIT: FORGOT, EVEN IF YOU POINT IT AT SKY IT WOULD RETURN VECTOR3, so this solution should work

2 Likes

It works! but the midpoint didnt seems to be correct. But i got what I want now, thank you!

1 Like

Glad to help you, you should mark my solution as solved

Already marked. Thank you for your help!

1 Like