Mouse not loading in LocalScript

The mouse gives an error of nil everytime despite waiting for it to load and using a LocalScript

I’ve tried doing a repeat like this

repeat task.wait()
	print("looking for mouse")
	mouse = localPlayer:GetMouse()
until mouse
	print("found")

And putting it inside of my functions like this

local function move()
	mouse = localPlayer:GetMouse()
	local MousePosition = mouse.Hit.Position
	ball.Position = MousePosition
	clickfast:Play()
	mouseClicked:FireServer(MousePosition, ball)
end

use this instead:

local Players = game:GetService("Players")
local player = Players.LocalPlayer

-- Ensure the player's mouse is available
local mouse
repeat
    task.wait()
    print("Waiting for mouse...")
    mouse = player:GetMouse()
until mouse
print("Mouse found!")

local function move()
    if not mouse then
        warn("Mouse is not available!")
        return
    end

    local MousePosition = mouse.Hit and mouse.Hit.Position
    if not MousePosition then
        warn("Mouse.Hit is nil!")
        return
    end

    ball.Position = MousePosition
    clickfast:Play()
    mouseClicked:FireServer(MousePosition, ball)
end

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