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

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

Asked by 4 years ago
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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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 
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Answer this question