Attempting to index nil with 'GetMouse'

Hello!
In my script it says attempting to index nil with ‘GetMouse’ I was trying to fix it and i failed, can someone pls fix it

local chips = script.Parent
local Mouse = player:GetMouse()
player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

local function colorChange(player)
	wait(2)
	local char = player.Caracter or player.CharacterAdded:Wait(0)
	if char then
		chips["Body Colors"].Parent = char
	    wait(1)
		char:Destroy()
	end
end

if chips.Enabled == true then
	Mouse.Button1Down:Connect(colorChange(player))
end

thanks for all help

1 Like

You did not define what player is yet. Your trying to get the mouse of “nil”

local Mouse = player:GetMouse()
player = game.Players:GetPlayerFromCharacter(script.Parent.Parent

you didnt define ‘player’ before : GetMouse () and define ‘player’ after

here is the fixed code :

local chips = script.Parent
player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
local Mouse = player:GetMouse()

local function colorChange(player)
	wait(2)
	local char = player.Caracter or player.CharacterAdded:Wait(0)
	if char then
		chips["Body Colors"].Parent = char
	    wait(1)
		char:Destroy()
	end
end

if chips.Enabled == true then
	Mouse.Button1Down:Connect(colorChange(player))
end

it does not work. I tried with your script, but it does not matter if you use mine or yours because its a global variable. I didn’t put local befor it.

Is it in a server script? As you can only use GetMouse in a local script.

local chips = script.Parent
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
local Mouse = player:GetMouse()

local function colorChange(player)
	wait(2)
	local char = player.Character or player.CharacterAdded:Wait(0)
	if char then
		chips["Body Colors"].Parent = char
	    wait(1)
		char:Destroy()
	end
end

if chips.Enabled == true then
	Mouse.Button1Down:Connect(colorChange(player))
end

Hello! I see you’re having trouble with your script. It looks like the issue is that you’re trying to use the GetMouse method on a player variable that hasn’t been defined yet. You should move the line local Mouse = player:GetMouse() below the line where you define the player variable.

Here’s an example of how you could fix your script:

local chips = script.Parent
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
local Mouse = player:GetMouse()

local function colorChange(player)
    wait(2)
    local char = player.Character or player.CharacterAdded:Wait()
    if char then
        chips["Body Colors"].Parent = char
        wait(1)
        char:Destroy()
    end
end

if chips.Enabled == true then
    Mouse.Button1Down:Connect(function() colorChange(player) end)
end

“In my script it says attempting to index nil with ‘GetMouse’ I was trying to fix it and i failed” - Don’t worry, we all make mistakes when coding. The important thing is to keep trying and learning from our mistakes! :+1:

1 Like

That’s literally what i posted XD

1 Like

Oh… I didn’t realize haha. Sorry about that.

1 Like
game.Players:GetPlayerFromCharacter(script.Parent.Parent)

maybe ‘script.Parent.Parent’ is not a valid Player Character and ‘game.Players:GetPlayerFromCharacter(script.Parent.Parent)’ return nil

1 Like

Just so you know, RBXScriptSignal:Wait() does not have parameters, so the 0 is doing nothing.

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