"attempt to call global (a nil value)" Error Help

In my script, I have a function at the bottom called move(). At the top of the script, I have a checking thing to make sure a player has the correct card to open a door. Once it checks out, it calls move() perfectly fine. However, when I call move() at the very top of the script it throws the error attempt to call global ‘move’ (a nil value) when I don’t think it should.

move() -- Error. If I remove it, the script works perfectly fine with the move() down below
local part = script.Parent
local door1 = script.Parent.Parent.Thing
local card = script.Parent.Parent.Card.Value
local debounce = false
part.Touched:connect(function(hit)
	wait()
    local char = hit.Parent
	if char:IsA("Tool") then
		char = char.Parent
	elseif char.Parent:IsA("Tool") then
		char = char.Parent.Parent
	end
    if char:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(char)
		if not player then print("No player")return end
		if char:FindFirstChildWhichIsA("Tool") then
			local equipped
			if char:FindFirstChildWhichIsA("Tool").Name:match("Key") then
				equipped = tonumber(char:FindFirstChildWhichIsA("Tool").Name:match("%d+"))
			elseif char:FindFirstChildWhichIsA("Tool").Name:match("Omni") then
				equipped = "Omni"
			end
			if equipped == "Omni" or card <= equipped then
				if debounce == true then return end
				debounce = true
				print("move")
				move()
			end
		end
    end
end)

I think the error here is that you’re trying to call the move function before declaring it. It would be like telling the system the punchline of a joke without context - it doesn’t make sense! Can you show where you defined the move function?

1 Like

Like @Deferend said, you’ll need to put the move() function above where you want to run it.

1 Like

I have the move function right below all of that I put in their. If I remove the move() at the top and let it go through all the checks, there is another move() near the end of the script that I put and that one works fine without any errors.

That’s what I’m saying. The move function should be defined above the code you want the run. The move() at the end of the script works because the function is defined.

1 Like

At the end of the script I put there, move() is still above the function and it works fine.image

Can you show us the code with the move function in it?

That’s because the part.Touched function isn’t fired before the move function is defined.

What is even in the move function?

This is the whole script.


move()
local part = script.Parent
local door1 = script.Parent.Parent.Thing
local card = script.Parent.Parent.Card.Value
local debounce = false
part.Touched:connect(function(hit)
	wait()
    local char = hit.Parent
	if char:IsA("Tool") then
		char = char.Parent
	elseif char.Parent:IsA("Tool") then
		char = char.Parent.Parent
	end
    if char:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(char)
		if not player then print("No player")return end
		if char:FindFirstChildWhichIsA("Tool") then
			local equipped
			if char:FindFirstChildWhichIsA("Tool").Name:match("Key") then
				equipped = tonumber(char:FindFirstChildWhichIsA("Tool").Name:match("%d+"))
			elseif char:FindFirstChildWhichIsA("Tool").Name:match("Omni") then
				equipped = "Omni"
			end
			if equipped == "Omni" or card <= equipped then
				if debounce == true then return end
				debounce = true
				print("move")
				move()
			end
		end
    end
end)

function move()
	print("moving")
	local opened = script.Parent.Parent.Opened.Value
	local tween = game:GetService("TweenService")
	local yuh = {}
	if opened == false then
		yuh.Orientation = door1.Orientation - Vector3.new(0,90,0)
		local tweeninfo = TweenInfo.new(1.5,Enum.EasingStyle.Back,Enum.EasingDirection.Out,0,false,0)
		local open = tween:Create(door1, tweeninfo, yuh)
		open:Play()
		open.Completed:connect(function()
			script.Parent.Parent.Opened.Value = true
		end)
	elseif opened == true then
		yuh.Orientation = door1.Orientation + Vector3.new(0,90,0)
		local tweeninfo = TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)
		local open = tween:Create(door1, tweeninfo, yuh)
		open:Play()
		open.Completed:connect(function()
			script.Parent.Parent.Opened.Value = false
		end)
	end
	wait(0.5)
	debounce = false
end

Simple issue, move isn’t registered when move is called at the top.

This should work instead

local part = script.Parent
local door1 = script.Parent.Parent.Thing
local card = script.Parent.Parent.Card.Value
local debounce = false
part.Touched:connect(function(hit)
	wait()
    local char = hit.Parent
	if char:IsA("Tool") then
		char = char.Parent
	elseif char.Parent:IsA("Tool") then
		char = char.Parent.Parent
	end
    if char:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(char)
		if not player then print("No player")return end
		if char:FindFirstChildWhichIsA("Tool") then
			local equipped
			if char:FindFirstChildWhichIsA("Tool").Name:match("Key") then
				equipped = tonumber(char:FindFirstChildWhichIsA("Tool").Name:match("%d+"))
			elseif char:FindFirstChildWhichIsA("Tool").Name:match("Omni") then
				equipped = "Omni"
			end
			if equipped == "Omni" or card <= equipped then
				if debounce == true then return end
				debounce = true
				print("move")
				move()
			end
		end
    end
end)

function move()
	print("moving")
	local opened = script.Parent.Parent.Opened.Value
	local tween = game:GetService("TweenService")
	local yuh = {}
	if opened == false then
		yuh.Orientation = door1.Orientation - Vector3.new(0,90,0)
		local tweeninfo = TweenInfo.new(1.5,Enum.EasingStyle.Back,Enum.EasingDirection.Out,0,false,0)
		local open = tween:Create(door1, tweeninfo, yuh)
		open:Play()
		open.Completed:connect(function()
			script.Parent.Parent.Opened.Value = true
		end)
	elseif opened == true then
		yuh.Orientation = door1.Orientation + Vector3.new(0,90,0)
		local tweeninfo = TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)
		local open = tween:Create(door1, tweeninfo, yuh)
		open:Play()
		open.Completed:connect(function()
			script.Parent.Parent.Opened.Value = false
		end)
	end
	wait(0.5)
	debounce = false
end
move()
4 Likes

When you remove the move() at the top… the script doesn’t error at all?

1 Like

Alright. Do you know the reason this happens tho? Like one errors, another one doesn’t.

Yes, the first time move() was called it wasn’t registered.

print(f) -- nil
function f()end
print(f) -- function: XXXXXXX
1 Like