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

Why does it give me an error saying: attempt to index nil with 'GetMouse'?

The code is inside a script of a VehicleSeat

local seat = script.Parent
local LightPart = script.Parent.Parent.LightParts
local on = false

seat.Changed:Connect(function(ch)
	if ch == "Occupant" then
		if seat.Occupant then
			local player = game.Players:FindFirstChild(seat.Occupant.Parent.Name) --The occupant is the humanoid, the humanoid.Parent is the player's character

			--Your code here
			game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key)
				if key == "l" then
					if on == true then
						on = false
						LightPart.HeadLightPart.Material = Enum.Material.SmoothPlastic
						LightPart.HeadLightPart2.Material = Enum.Material.SmoothPlastic
					else
						LightPart.HeadLightPart.Material = Enum.Material.Neon
						LightPart.HeadLightPart2.Material = Enum.Material.Neon
					end
				end
			end)
		end
	end
end)

6 comments

0

Is this a Server Script?

0

LocalPlayer doesn't work in Server Scripts. Instead of game.Players.LocalPlayer you should use your already defined variable "player"

0

I posted a LocalScript version on your previous question, use that instead

0

It only works if I put it inside StarterCharacterScripts, then how am I gonna make the keybind work?

0

You can put it inside StarterPack, StarterPlayerScripts (and StarterGui) too

0

I'm trying to make a cars headlights turn on, and what if there's another car with the same model name?

1 answer

game.Players.LocalPlayer.Character.Humanoid.Seated:Connect(function(val, seat)

	--val is true or false, based on the player is sitting or not
	--seat is the Seat part that player sat on

end)

You should use this code (official article). The seat is an Instance, so you can specify the vehicle through that.

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