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

I need help on how to make your gun to follow your cursor?

I've been joining some games and I need help on how to make you gun follow your cursor move side to side and up and down. But, I just can't seem to figure it out I keep researching and trying to learn how to code that.

Example: http://www.roblox.com/Apocalypse-Night-place?id=93307312

This is what I'm talking about.

1 answer

Hmm try this. This is a piece of code I modified. But it only works with one handed weapons. If you want two handed weapons, I would try asking someone else. As I do not know how to do two hands.

Essentially, to make your gun follow the cursor, you need to make the handle hook to the camera, basically. I don't know how to explain it in English, and I speak English. The script should be a local script, by the way.

function WaitForChild(parent,...)
	local debugPrint = false
	for _, i in ipairs({...}) do
		if type(i)=='boolean' then
			debugPrint = i
		else
			while not parent:FindFirstChild(i) do  
				wait(1/30) 
				if debugPrint then
					print(script.Name..':'..parent.Name..' Waiting for '.. i)
				end
			end
			parent=parent[i]
		end
	end
	return parent
end

function ForEach(parent,func)

end

function MakeValue(class,name,value,parent)

end	

function TweenProperty(obj, propName, inita, enda, length,sentinel)

end


local Tool = script.Parent
local Handle = WaitForChild(Tool,'Handle')

local equipped = false
local rayparts = {}

local oldC0 =nil
local nweld


Tool.Equipped:connect(function(mouse)
	Spawn(function()
		local colorChoice = 1
		if not Tool.Parent:FindFirstChild('Torso') or not Tool.Parent.Torso:FindFirstChild('Right Shoulder') or not Tool.Parent:FindFirstChild('Humanoid') then return end
		nweld = Tool.Parent.Torso['Right Shoulder']
		oldC0 = nweld.C0
		nweld.CurrentAngle = 0
		nweld.DesiredAngle = 0
		nweld.MaxVelocity = 0
		equipped = true
		while equipped and Tool.Parent:FindFirstChild('Torso') do
			local tframe = Tool.Parent.Torso.CFrame
			tframe = tframe + tframe:vectorToWorldSpace(Vector3.new(1, 0.5, 0))
			local taim = mouse.Hit.p -( tframe.p )
			nweld.C0 = (CFrame.new(Vector3.new(),tframe:vectorToObjectSpace(taim))*CFrame.Angles(0,math.pi/2,0))+Vector3.new( 1, 0.5, 0 )
			
			
			wait()
		end
		nweld.C0 =oldC0
		
	end)
end)

Tool.Unequipped:connect(function(mouse)
	equipped= false
	if nweld then
		nweld.MaxVelocity =.15
		nweld.C0 =oldC0
	end
	
end)

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