Why isn't my Player:GetMouse() not working?

I tried doing Player:GetMouse() but the output says:
Workspace.Rapideed.FireBallscript.Fireball:10: attempt to index nil with 'GetMouse'
I’m not sure why this doesn’t work for some reason. This script is in StarterCharacterScript
Script:

local Player = game.Players:GetPlayerFromCharacter(script.Parent)

local mouse = Player:GetMouse() --Line 10
local MousePosition = mouse.Hit.p

Thank you for reading.

2 Likes

The error you’re receiving is because Player is being set to nil, aka :GetPlayerFromCharacter(script.Parent) isn’t returning a Player object from that model, so it sets local Player to nil

Also, is it a LocalScript or Script?
https://developer.roblox.com/en-us/api-reference/function/Player/GetMouse

1 Like

It’s a local script. But, for some reason there is no error even though you said it doesn’t work.

If it’s a LocalScript why not just do

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

?

2 Likes

Ok I will try this but it still doesn’t explain why I have a mouse error output.

The error is because you’re trying to essentially do the following

local Player = nil --When it can't find the character for whatever reason.

local mouse = nil:GetMouse() --This isn't a function of nil
1 Like

If that player variable was wrong how could it fire the mouse object? But I will try.

That’s what I’m saying, it’s not setting Player to be the actual current player, that’s why it’s saying attempt to index nil with 'GetMouse'

1 Like

then

local mouse = game:GetService('Players').LocalPlayer:GetMouse()
2 Likes

This would work too, but making Player a variable opens it up for use later on in the script.

3 Likes

I’m pretty sure getmouse can only be used on the client, also getting the player from the character might return nil sometimes.

Here’s a basic example of how you would use it.

local Players = game:GetService('Players')

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

local part, debounce = script.Parent, false

local placement = part:Clone()
placement.CFrame = CFrame.new()
placement.Transparency = .8
placement.CanCollide = false
placement.Anchored = true
placement.Parent = workspace

mouse.Move:Connect(function()
    placement.Position = mouse.Hit.Position  
end)

mouse.Mouse1Down:Connect(function()
    if debounce then
        return
    end

    debounce = true
    print('Position:', mouse.Hit.Position)
    
    local clone = part:Clone()
    clone.CFrame = mouse.Hit
    clone.Parent = workspace

    wait(2)

    debounce = false
    clone:Destroy()
end)
1 Like

Actually I see the problem this might work:

local Player = game.Players.LocalPlayer -- the original might not have worked, but game.Players.LocalPlayer always gets the player.
local mouse = Player:GetMouse()
local MousePosition = mouse.Hit.p
1 Like

I will test this sorry for my late response.

Thank you for everyone who helped I tested your solutions and it worked great thanks! Since I wasn’t able to test at the time everyone had a correct solution so sorry if I can’t directly give you it.