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

Calling a function returns an error?

I am creating a script to prevent players from teleporting to long distances. There are many functions, but when I try to call this specific one, all functions return an error "attempt to call global (a nil value)". I tried to copy and paste the function in another script and it still gives the same error for that function.

This is the script:

local plr = game.Players.LocalPlayer
repeat wait() until plr.Character and plr.Character:FindFirstChild("Torso") and plr.Character:FindFirstChild("Humanoid")
local char = plr.Character
local hum = char.Humanoid

local MaxDistance = 250

local LastLocation = nil
local PASS = true

repeat wait(5) CheckPlayerLocation() until PASS == false

function CheckPlayerLocation()
	if PASS and char ~= nil then
		if LastLocation ~= nil then
			local CurrentLocation = char.HumanoidRootPart.Position
			local Distance = (CurrentLocation-LastLocation).Magnitude
			if Distance > MaxDistance then
				PASS = false
				-- kicks player --
				return
			else
				LastLocation = char.HumanoidRootPart.Position
			end
		else
			LastLocation = char.HumanoidRootPart.Position
		end
	end
end

I don't understand why it considers it a "nil value", it looks fine to me, and every other function has no problem running when the "repeat wait(5) CheckPlayerLocation() until PASS == false" is deactivated. Maybe I'm doing something wrong with this part? If so I don't understand what that might be..

3 comments

1

It's because you call the function on line 11, but you don't make it until line 13, so on line 11 you call a function that doesn't exist

0

Oh yeah, I forgot about that. Thank you.

0

I can't mark this as answered because you wrote it as a comment, and it doesn't let me mark my own answer as correct

2 answers

This is because you're not defining your function!

after a function like that, you need to define it by writing at the end of it

[Insert Defining code here]:Connect(CheckPlayerLocation)

and by defining code I mean what it's checking for to be activated.

1 comment

0

That's for events, this is just a loop repeating that function. Anyways I know why it didn't work now

The function has to be called after it's defined. For some reason I forgot about that.. Somehow all other functions were able to be called even before the function definition but that's probably because they are triggered by events, and so use :Connect.

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