Help with forward shooting gun

so im trying to make a gun that shoots in direction of mouse in normal but when its used by a vr player its shoots straight, just the problem is that im terribly bad at math and i dont know how to make it shoot straight out of the barrel this is the code i have so far
also i switched the vr and pc check bc its easier to test in pc

local script:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local vrservice = game:GetService('VRService')

script.Parent.Activated:Connect(function()
	if vrservice.VREnabled == false then
		script.Parent.Events.ShootRE:FireServer(mouse.Hit)
	else
		script.Parent.Events.ShootRE:FireServer("vr")
	end
end)

server script:




script.Parent.Events.ShootRE.OnServerEvent:Connect(function(plr,mousehit)
	
	script.Parent.Handle.FireSound:Play()
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {plr.Character}
	local result
	if mousehit == "vr" then
		result = workspace:Raycast(script.Parent.Handle.Attachment.WorldPosition,(mousehit.Position - script.Parent.Handle.Attachment.WorldPosition).Unit * 1000,params)
	else
		result = workspace:Raycast(script.Parent.Handle.Attachment.WorldCFrame.Position,(script.Parent.Handle.Attachment.WorldCFrame.LookVector).Unit * 100,params)
	end
	print(result)
	if result and result.Instance.Parent == workspace.Lui then
		local blood = game.ReplicatedStorage.Blood:Clone()
		blood.Parent = result.Instance
		blood.Position = result.Position
		local weld = Instance.new("WeldConstraint")
		weld	.Parent = blood
		weld	.Part0 = blood
		weld.Part1 = result.Instance
		blood.Blood3.Enabled = true
		local bodyvelocity = Instance.new("BodyVelocity")
		bodyvelocity.MaxForce = Vector3.new(1e9,1e9,1e9)
		bodyvelocity.Parent = result.Instance
		bodyvelocity.Velocity = CFrame.new(result.Position,result.Position+result.Normal).LookVector * -10
		game:GetService("Debris"):AddItem(bodyvelocity,0.1)
		game:GetService("Debris"):AddItem(blood,0.5)
		local hole = game.ReplicatedStorage.Hole:Clone()
		hole.Parent = result.Instance
		hole.CFrame = CFrame.new(result.Position,result.Position + result.Normal)
		local weld2 = Instance.new("WeldConstraint")
		weld2.Parent = hole
		weld2.Part0 = hole
		weld2.Part1 = result.Instance
		workspace.Lui.Humanoid:TakeDamage(100)
	end
end)

1 Like

also when i print out result it prints out righthand wich the gun is held in so theres a problem with the direction and all i want it to just shoot in the direction of attachment in the gun

Basically, when mousehit is "vr", you’re trying to get mousehit.Position within the if mousehit == "vr" then

Maybe that is the reason (Just something I saw when trying to figure it out)

no im making an if statement to check if it sends a mouse position or the vr string if its vr then it will just shoot a straight ray and if its not u shoot it relative to the mouse position but the problem is it keeps saying it hit the baseplate even tough its supposed to hit the npc or the wall

when you say swapped, did you mean in the server script, or in the client script?

You may be trying to access the mousehit variable when mousehit is “vr”, and so it throws an error.

im not really that keen on raycasts, so that’s my only suggestion as of now

edit - context:
result = workspace:Raycast(script.Parent.Handle.Attachment.WorldPosition,(mousehit.Position - script.Parent.Handle.Attachment.WorldPosition).Unit * 1000,params)

you are using the mousehit variable (which at this point is equal to “vr”) in the variable.

No with swapped i ment like it has 2 things if its vr then it will do the thing where it shoots in straight line else it will use mouse pos wig swapped i ment that the dtraight line is now when the player is not vr
To test easier but it doesnt throw an error the raycasting just isnt working well

I dont know how to make it go into a straight line because it hits the baseplate even tough im. Pointing at a wall