Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Calling a function returns an error?

Asked by
LeHelary 142
4 years ago

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

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 Spjureeedd 385 — 4y
0
Oh yeah, I forgot about that. Thank you. LeHelary 142 — 4y
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 LeHelary 142 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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.

0
That's for events, this is just a loop repeating that function. Anyways I know why it didn't work now LeHelary 142 — 4y
Ad
Log in to vote
0
Answered by
LeHelary 142
4 years ago

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.

Answer this question