Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

(SOLVED) How do I make this gun fire where the mouse cursor is?

THE PROBLEM: The guns projectile does not go where the mouse cursor is looking, I have researched this for a good few hours and have no clue what to do, if someone could point me in the right direction. (I don't need the gun to rotate with the player I just want the projectile to shoot from the mouses location)

HOW THE GUN WORKS It takes a projectile from a folder in workspace and clones it, the projectile has code to move itself forward and also damage players on impact. currently I am setting the clones position to the position of a part attached to the gun tool currently for testing purposes.

SCRIPT

Gun firing script:

local tool = script.Parent
local handle = tool:WaitForChild('Handle')
local FirePort = script.Parent.Handle.Real_Fire_Part
local ammo = game.ReplicatedStorage.Hyper_Ammo_Sniper

tool.Equipped:Connect(function()
	tool.Equip.Playing = true
	wait(2)
	tool.Equip.Playing = false
end)

tool.Activated:Connect(function()
	if ammo.Value > 0 then
		print("phase1")
		local bullet = game.Workspace.Projectiles.Hyper_Sniper_bullet:Clone()
		bullet.Parent = game.Workspace
		bullet.Anchored = true
		bullet.Position = script.Parent.Handle.Real_Fire_Part.Position
		bullet.Orientation = FirePort.Orientation
	
		tool.Fire.Playing = true
		ammo.Value = ammo.Value - 1
		print("phase2")
		
	end
end)

Please help i've got no clue what to do here.

2 comments

0

i know your never going to read this but on line 7 you could have done tool.Equip:Play() and get rid of lines 8 and nine but only if the sound is 2 seconds long if it is not don't change a thing buddy

0

also i need to know how to script a bullet i use bodyposition but it gets weird if the thing goes 1000 so either an inaccurate bullet or a slow bullet

1 answer

✓ Accepted answer

I'm going to assume you want to create a weapon that fires projectiles instead of raycasting, which I personally don't recommend, but to each their own.

Your code seems good. The reason the bullet does not go forward is because your firepart is likely facing the wrong way. Try changing the direction of your firepart, and seeing if that fixes it.

3 comments

0

Thanks! it is firing in a straight line however I think that I may replace the system with one that uses raycasting to improve aiming. Do you happen to know of a way to make the bullet follow the path of a raycast? If so thanks!

0

nevermind I figured it out and the gun works perfectly.

0

if you need more help with raycasting feel free to message me. If this question was what you were looking for, please mark this post as the answer. Good luck with your game

This discussion is preserved as it was. New answers, comments, and votes are closed.