Help with mouse position

I’m trying to make a script that when the player moves their mouse the “swivel parts” are supposed to face towards the mouse position. I am having an error trying to retrieve the position?

Workspace.20mm.Handler:33 attempt to index nil with 'Hit'
local RunService = game:GetService("RunService")

local proximityPrompt = script.Parent.Swivel.CenterPart.ProximityPrompt
local anchoragePoint = script.Parent.Swivel.AnchoragePoint
local lowerAnchoragePoint = script.Parent.Swivel.LowerAnchoragePoint

local leftBarrel = script.Parent.Barrels.LeftBarrel
local rightBarrel = script.Parent.Barrels.RightBarrel

local animation = script.Animation

proximityPrompt.Triggered:Connect(function(player)
	local humanoid = player.Character:FindFirstChild("Humanoid")

	if humanoid then
		local track = humanoid:LoadAnimation(animation)

		track.Priority = Enum.AnimationPriority.Idle

		player.Character.HumanoidRootPart.CFrame = anchoragePoint.CFrame

		track:Play()

		player.Character.Humanoid.AutoRotate = false
		player.Character.HumanoidRootPart.Anchored = true
		player.Character.LowerTorso.CFrame = lowerAnchoragePoint.CFrame
		proximityPrompt.Enabled = false		
		
		local function setPosition() 
			player.Character.LowerTorso.CFrame = lowerAnchoragePoint.CFrame
		end

		local mouse = player:GetMouse()

		local mousePosition = mouse.Hit.Position

		RunService.Heartbeat:Connect(function()
			local primaryPartPosition, mousePosition = script.Parent.Swivel.PrimaryPart.Position, mouse.Hit.Position
			primaryPartPosition.CFrame = CFrame.new(primaryPartPosition, Vector3.new(mousePosition.X, primaryPartPosition.Y, mousePosition.Z))
		end)

		while humanoid do
			task.wait()
			script.Parent.Swivel.PrimaryPart.CFrame = CFrame.new(script.Parent.Swivel.PrimaryPart.Position, Vector3.new(mousePosition.X, script.Parent.Swivel.PrimaryPart.Position.Y, mousePosition.Z))
			setPosition()
		end
	end
end)

image
image

local RunService = game:GetService("RunService")

-- (replace with actual names)
local leftSwivel = script.Parent.Swivel.LeftSwivel
local rightSwivel = script.Parent.Swivel.RightSwivel

-- to update swivel rotation based on mouse position
local function updateSwivelRotation(mouse)
    if not mouse.Hit then 
        return
    end

    local targetPosition = mouse.Hit.Position
    local currentSwivelPosition = leftSwivel.Position 

  
    local direction = targetPosition - currentSwivelPosition

  
    local newCFrame = CFrame.lookAt(currentSwivelPosition, targetPosition)

    --  (adjust based on your setup)
    leftSwivel.CFrame = newCFrame
    rightSwivel.CFrame = newCFrame
end

RunService.Heartbeat:Connect(function()
    local mouse = game:GetService("UserInput").Mouse
    updateSwivelRotation(mouse)
end)
2 Likes

There is no “LeftSwivel” or “RightSwivel” parented to “Swivel”?

local leftSwivel = script.Parent.Swivel.LeftSwivel – Replace with actual names
local rightSwivel = script.Parent.Swivel.RightSwivel – Replace with actual names

My apologies., I forgot to mention the below lines in my script are not related to the swivel feature of the model.

local leftBarrel = script.Parent.Barrels.LeftBarrel
local rightBarrel = script.Parent.Barrels.RightBarrel

Edited to:

local RunService = game:GetService("RunService")

local proximityPrompt = script.Parent.Swivel.CenterPart.ProximityPrompt
local anchoragePoint = script.Parent.Swivel.AnchoragePoint
local lowerAnchoragePoint = script.Parent.Swivel.LowerAnchoragePoint

local animation = script.Animation

roximityPrompt.Triggered:Connect(function(player)
	local humanoid = player.Character:FindFirstChild("Humanoid")
	
	if humanoid then
		local track = humanoid:LoadAnimation(animation)
		
		track.Priority = Enum.AnimationPriority.Idle
			
		player.Character.HumanoidRootPart.CFrame = anchoragePoint.CFrame

track:Play()
		
		player.Character.Humanoid.AutoRotate = false
		
		player.Character.HumanoidRootPart.Anchored = true
		
		player.Character.LowerTorso.CFrame = lowerAnchoragePoint.CFrame
		
		proximityPrompt.Enabled = false		

local function setPosition() 
			player.Character.LowerTorso.CFrame = lowerAnchoragePoint.CFrame
		end


		RunService.Heartbeat:Connect(function()
			local mouse = player:GetMouse()
			
			if mouse.Hit then
				local mousePosition = mouse.Hit.Position
			
				local primaryPartPosition, mousePosition = script.Parent.Swivel.PrimaryPart.Position, mouse.Hit.Position
				primaryPartPosition.CFrame = CFrame.new(primaryPartPosition, Vector3.new(mousePosition.X, primaryPartPosition.Y, mousePosition.Z))
			end
		end)
	end
end)

But receiving new error of

Workspace.20mm.Handler:37: attempt to index nil with 'Hit'

Message edit: I think I can only use player:GetMouse() on a local script?

yes u can use player get mouse on a local script

for server side u might use remote functions or events something like that to of:

local remoteEvent = script.Parent:WaitForChild("RemoteEventName") -- Replace with actual remote event name

remoteEvent.OnClientEvent:Connect(function(player, mousePosition)

  -- you receive the mousePosition from the client
end)
1 Like

The turret’s CFrame is now the opposite of the mouse position?

Local Script

-- Services
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

-- Remote Events
local remoteEventsFolder = ReplicatedStorage:WaitForChild("RemoteEvents")
local turretInputTriggeredEvent = remoteEventsFolder:FindFirstChild("TurretInputTriggered")

-- Objects
local player = Players.LocalPlayer
local mouse = player:GetMouse()

RunService.RenderStepped:Connect(function()
	turretInputTriggeredEvent:FireServer(mouse.Hit.Position)
end)

Handler

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEventsFolder = ReplicatedStorage:WaitForChild("RemoteEvents")

local turretInputTriggeredEvent = remoteEventsFolder:FindFirstChild("TurretInputTriggered")

local proximityPrompt = script.Parent.Swivel.CenterPart.ProximityPrompt
local anchoragePoint = script.Parent.Swivel.AnchoragePoint
local lowerAnchoragePoint = script.Parent.Swivel.LowerAnchoragePoint

local animation = script.Animation

proximityPrompt.Triggered:Connect(function(player)
	local humanoid = player.Character:FindFirstChild("Humanoid")

	if humanoid then
		local track = humanoid:LoadAnimation(animation)

		track.Priority = Enum.AnimationPriority.Idle

		player.Character.HumanoidRootPart.CFrame = anchoragePoint.CFrame

		track:Play()

		player.Character.Humanoid.AutoRotate = false

		player.Character.HumanoidRootPart.Anchored = true

		player.Character.LowerTorso.CFrame = lowerAnchoragePoint.CFrame

		proximityPrompt.Enabled = false		
	

		local function setPosition() 
			player.Character.LowerTorso.CFrame = lowerAnchoragePoint.CFrame
		end

		turretInputTriggeredEvent.OnServerEvent:Connect(function(player, mousePos)
			local primaryPart = script.Parent.Swivel.PrimaryPart
			primaryPart.CFrame = CFrame.new(primaryPart.Position, Vector3.new(mousePos.X, mousePos.Y, mousePos.Z))
		end)
	end
end)

Well what is the exact issue again right and then send the desired effect to fix that issue or if it’s not an error what is it or anything really

There is no errors from the script. I want the turret front to point towards the position of the mouse and right now it’s pointing the opposite direction.

Server side adjustment here

turretInputTriggeredEvent.OnServerEvent:Connect(function(player, mousePos)
    local primaryPart = script.Parent.Swivel.PrimaryPart
    -- Directly use the mouse position to make the turret face towards it
    primaryPart.CFrame = CFrame.lookAt(primaryPart.Position, mousePos)
end)

This change uses CFrame.lookAt()

Make sure the rest of your server-side script remains unchanged

1 Like

No ideas why, but it still makes the direction opposite to the mouse position.

If it still does you can try inverting it by that case scenario if it always does it like that

turretInputTriggeredEvent.OnServerEvent:Connect(function(player, mousePos)
    local turretPart = script.Parent.Swivel.PrimaryPart
    local turretPos = turretPart.Position
    -- Calculate direction vector
    local direction = (mousePos - turretPos).unit

    -- Invert the direction if it points opposite to the mouse
    -- This could involve flipping the direction vector if necessary
    -- Example: direction = -direction -- This inverts the direction

    -- Alternatively, if flipping the direction doesn't work directly, calculate the new CFrame manually
    local newLookAtPosition = turretPos + direction
    
    -- Set the turret's CFrame to face the new, corrected position
    turretPart.CFrame = CFrame.lookAt(turretPos, newLookAtPosition)
end)
1 Like

Remember to try both the options and uncomment to see if one of them works best for it

1 Like

I used

                        local turretPart = script.Parent.Swivel.PrimaryPart
			local turretPos = turretPart.Position
			local direction = (mousePos - turretPos).unit
			
			local newLookAtPosition = turretPos - direction

			turretPart.CFrame = CFrame.lookAt(turretPos, newLookAtPosition)

to make it work, thank you very much for your help.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.