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

Why is the local script not seeing the function?

I have a localscript, and the first line is getmousething(). Later in the script, I have:

function getmousething()
	local player = game.Players.LocalPlayer
	local mouse = player:GetMouse()
	mouse.KeyDown:connect(function(key)
		if key.Byte == 102 then
			print 'F presseed'
		end
	end)
end

The error message is attempt to call global 'getmousething' (a nil value). Why can't it find that function?

2 answers

✓ Accepted answer

This might fix it.

function getmousething()
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key)
if key.Byte == 102 then
print 'F presseed'
end
end)
end
getmousething() -- Roblox must read the function before having it be called.

--You said that you ran it before it was called, Like above the function which would have been the error you got.

Roblox reads or loads everything in a script from Top to Bottom. So if your first line of Code is the call for the function, It hasn't read the function yet so it calls an error.

Try putting your call right under the function, or at the very bottom of the script.

5 comments

0

I'll try something to fix it I guess

0

This is incorrect. If you have a function present anywhere within the script, it will fun the function. People put them at the top purely for convenience.

0

Why did i get a negative point for telling you the correct answer... Go ask anybody else and they'll say im right. Just trying to help :l

0

Alright if you think your right, Go make a function that is at the bottom of your script and put the caller at the top.. I just tested this 2 minutes ago and it called an error because the function was nil....

0

@Necrorave Not true, Kevin is correct. I had a problem calling a function inside a function because the function being called was below the function that called the other one.

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