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

Returning something using a function through a pcall won't work?

local ProductFunctions = {

[1] = function(player)
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		-- Heal the player to full health
		player.Character.Humanoid.Health = 0
		-- Indicate a successful purchase
		return true -- I WANT THIS TO RETURN 
	end
end,



}

local Handler = ProductFunctions[1]

local S,E = pcall(function()
	return Handler(game.Players:WaitForChild('Roblox')) -- SHOUDL RETURN TRUE 
	
end)

print(S,E) -- Should normally print true, true 

but its not printing true is there any way to print true

3 answers

The problem is in the table

If you gonna make a table it should be like this

local Table = {
	1,
	2,
	true,
	false,
	function(ToPrint) 
		print(ToPrint)
	end
}

--You can print each valor using Table[Index]

print(Table[1]) --1
print(Table[2]) -- 2
print(Table[3]) --true
print(Table[4]) --false
print(Table[5]("Hello")) --Hello

if you want to name the values ??in the table you would have to name them as if it were a variable

Example

local Table = {
	One = 1,
	Two = 2,
	Three = true,
	Four = false,
	Five = function(ToPrint) 
		print(ToPrint)
	end
}

--And you get the values ??using Table.Value

print(Table.One) --1
print(Table.Two) -- 2
print(Table.Three) --true
print(Table.Four) --false
print(Table.Five("Hello")) --Hello

The fixed code should be like this:

local ProductFunctions = {

One = function(player)
    if player.Character and player.Character:FindFirstChild("Humanoid") then
        -- Heal the player to full health
        player.Character.Humanoid.Health = 0
        -- Indicate a successful purchase
        return true -- I WANT THIS TO RETURN 
    end
end,



}

local Handler = ProductFunctions.One

local S,E = pcall(function()
    return Handler(game.Players:WaitForChild('Roblox')) -- SHOUDL RETURN TRUE 

end)

print(S,E) -- Should normally print true, true 

Or like this:

local ProductFunctions = {

	function(player)
	    if player.Character and player.Character:FindFirstChild("Humanoid") then
	        -- Heal the player to full health
	        player.Character.Humanoid.Health = 0
	        -- Indicate a successful purchase
	        return true -- I WANT THIS TO RETURN 
	    end
	end


}

local Handler = ProductFunctions[1]

local S,E = pcall(function()
    return Handler(game.Players:WaitForChild('Roblox')) -- SHOUDL RETURN TRUE 

end)

print(S,E) -- Should normally print true, true 

the problem is that the studio is getting confused if you are calling the first value in the table or the function so you should just call the function and you can only check who is in the game because you put player.Character, because roblox isn't in the game, it still wouldn't return true. instead try your name, like this

local productFunctions = {
["Heal"] = function(player)
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		player.Character.Humanoid.Health = 0
		return true
	end
end
}

local heal = productFunctions["Heal"]
local output

if heal then
	local success,errormsg = pcall(function()
		output = Handler(game.Players:WaitForChild("EasyNameToMemorize")
		return output
	end
end

if success and output then
	print(output) -- Because success is if the pcall was successful and not the function
end

okay, your code looks kind of peculiar.. try this

local ProductFunctions = {};

ProductFunctions[1] = function(player)
    if(type(player) == "userdata" and player:IsA("Player")) then
        return true
    else
        print("player is not a player")
    end
end


local handler = ProductFunctions[1];
local playerName = "Arsubia12" -- change this to the name of the target player
print("Getting the player") -- for debugging purposes
local player  = game.Players:WaitForChild(playerName);
print("player exists") --to confirm that player exists for debugging purposes

local s, e = pcall(handler, player);

print(s, e)

the output of the above code should look like this

"getting the player"

"player exists

true true

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