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 <FunctionName> (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..
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.
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.