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

How could i make my turret follow a players mouse? (Advanced Scripters)

I want this code to stay (below) because it works perfect. now I want to make it so the turret rotates in the position your mouse is. Is that better?

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.F then
	local mouse = game.Players.LocalPlayer:GetMouse()
	local gun = workspace.Turret.Gun2
	local gunPos= gun.Position
	local part = Instance.new("Part",workspace)
    part.Size = Vector3.new(1,1,2)
    part.Material = "SmoothPlastic"
    part.Position = gunPos
	part.CanCollide = false
	local bv = Instance.new("BodyVelocity", part)
	bv.Velocity = part.CFrame.lookVector * 820
	
    part.Touched:connect(function(hit)
    if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 3000
	end
    end)
    end	
		
		
		
		
		
		
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

2 comments

0

You're very vague. Can you please explain your problem? Also, try 'tabbing' your lines of code. Press tab on the pieces of code that are wrapped around a function or an if statement, please. It makes it easier for us to read.

0

ok i will try again

1 answer

✓ Accepted answer

You can use the Mouse.Hit CFrame to get the location within 3D space, then use .p to return the Vector3 Position component of the CFrame.

Applying the .Move signal of Mouse, we can catch when we move our cursor to essentially create a lookVector built from the cursor's X,Z coordinates for perpetual rotation. Combining the numerical constant of Y with the Turret Position, we can finally have the Turret rotate in three-hundred-sixty degrees where it stands, by adjusting it's CFrame accordingly.


Note:

When Scripting projectiles, it is always recommended to use DebrisService's :AddItem() method to schedule removal of the projectile after a given window. If we do not do this, we'll flood workspace with unnecessary parts.

A couple more things:

  1. Setting or comparing properties should always be done via Enums, it is a deprecated practice to use Strings; it is no-longer supported by ROBLOX.

  2. With creating new Instances through Instance.new(), you shouldn't provide the parental argument as it is also considered deprecated, and can cause potential performance issues. You should set the .Parent property outside the constructor—since you're also modifying the Object, you should set the parent last.

  3. When dealing with InputObjects, you should always debounce the GameProcessed parameter, as it allows core services to process the InputObject with a higher priority. This will prevent important preset keybindings from being interrupted.


local UserInputService = game:GetService("UserInputService")
local Debris = game:GetService("Debris")

local Player = game:GetService("Players").LocalPlayer
local Cursor = Player:GetMouse()

local Turret = workspace.Turret
local Gun = Turret.Gun2

local function FireProjectile(InputObject, GameProcessed)
	if (GameProcessed) then return end
	if (InputObject.KeyCode == Enum.KeyCode.F) then
		local Projectile = Instance.new("Part")
		Projectile.Size = Vector3.new(1,1,2)
		Projectile.Material = Enum.Material.SmoothPlastic
		Projectile.CanCollide = false
		Projectile.CFrame = Gun.CFrame
		Projectile.Parent = workspace

		local BodyVelocity = Instance.new("BodyVelocity")
	   	BodyVelocity.Velocity = Projectile.CFrame.lookVector * 100
		BodyVelocity.Parent = Projectile
		Debris:AddItem(Projectile, 5)

		pcall(function()
			Projectile.Touched:Connect(function(Hit)
				local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
				if (Humanoid and Projectile) then
					Humanoid:TakeDamage(Humanoid.Health)
					Projectile:Destroy()
				end
			end)
		end)
	end
end

Cursor.Move:Connect(function()
	local CursorPosition = Cursor.Hit.p
	local TurretPosition = Turret.Position
	local LookVector = Vector3.new(CursorPosition.X,TurretPosition.Y,CursorPosition.Z)
	TurretPosition.CFrame = CFrame.new(GunPosition, LookVector)
end)

UserInputService.InputBegan:Connect(FireProjectile)

If this helps, don't forget to accept this answer!

3 comments

0

Thank you so much!

0

The turret move rotate doesnt work pro because its welded? Idk im bad at scripting

0

wait ik the prob the turrent.Position doesnt work beaucs its a modle

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