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

Cant get player mouse. But I defined it. Is this correct? [Unsolved]

Script works, but it's telling me that "18:20:36.041 - Players.Player1.Backpack.Plane.Main:46: attempt to index global 'mouse' (a nil value)" When I Just defined it...


wait(0.1)	-- Yes, this is needed. If it's not here, the plane will only work once.
local player = script.Parent.Parent.Parent
local mouseSave
local maxBank = 90
local gyro = script.Parent.Parent.Parent.Character.Real.Main.Gyro

function fly(m)	-- Main function that controls all of the flying stuff. Very messy.
				inputService = game:GetService("UserInputService")

SPEED =0
while true do
	wait()
local enabled = true
Player = script.Parent.Parent.Parent
--Player:WaitForDataReady()
mouse = Player:GetMouse()		----RIGHT HERE D:
function onKeyDown(key)
if not enabled then return end
enabled = false
	Key = key:lower()
	if key == "w" then
		
		
			SPEED = SPEED +1
			print(SPEED)
		local main = script.Parent.Parent.Parent.Character.Real.Main
		local move = script.Parent.Parent.Parent.Character.Real.Main.Move
			move.velocity = (main.CFrame.lookVector + main.CFrame.lookVector*SPEED)		-- Set speed to aircraft
			
		local bank = ((((m.ViewSizeX/2)-m.X)/(m.ViewSizeX/2))*maxBank)	-- My special equation to calculate the banking of the plane. It's pretty simple actually
		bank = (bank < -maxBank and -maxBank or bank > maxBank and maxBank or bank)
			gyro.maxTorque = Vector3.new(math.huge,math.huge,math.huge)
			gyro.cframe = (m.Hit*CFrame.Angles(0,0,math.rad(bank)))

		
		
		end
		end
enabled = true
end
end

		
		
	mouse.KeyDown:connect(onKeyDown)		
	
		
		
		
		
		
script.Parent.Selected:connect(function(m)	-- Initial setup
	mouseSave = m
	game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Attach
	m.Icon = "http://www.roblox.com/asset/?id=48801855"	-- Mouse icon used in Perilous Skies
	delay(0,function() fly(m) end)	-- Basically a coroutine
end)



1 answer

To make it easier do something like this

local player = game.Players.LocalPlayer --saves you alot of time by not doing "script.Parent.Parent" and less errors
local mouse = player:GetMouse()

im assuming you're doing this in a local script, right?

Example

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

function fly()
	onKeyDown()
end

function onKeydown(key)
	if key == "w" then
		--while loop inside
	end --or something similar like this
end

mouse.KeyDown:connect(onKeyDown)

6 comments

0

Yes...

0

But the script still gives me the same error.

0

The only problem i see is that its inside the while loop. If i were you i'd leave it outside the loop and then call it and also add the loop inside the function

0

I edited the answer so you an see what im talking about

0

Wont work .-.

0

Really? Well ummm, the last thing i can only tell you is to rewrite your code (use the example above to make it look clean) but im guessing youre still going or might end up getting the same error. But before you do that, bring "mouse.KeyDown:connect(onKeyDown)" to the very last line of your code and define the "mouse" variable again but to the very first couple of lines before you make a function

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