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

module script won't recognize return statement? [SOLVED]

Asked by 4 years ago
Edited 4 years ago

I'm working on a simple "find x and return" type quest but I'm having issues. I have two module scripts, one for the clickable part and one for a table. In the table it recognizes the player has an existing tool when the part is clicked, but won't recognize my return statement. The item giver script is supposed to run the table inventorydata function (which it does properly) and recognizes the player has an existing tool, but it will not return the function. Here's the code:

Table script

local ItemModule = {}
local itemFolder = game.ServerStorage.ItemStorage

ItemModule.ItemStorage = { -- For cloning into the players backpacks in item module's scripts.

    ["Sample"] = itemFolder.Sample

}

function ItemModule.inventoryData(backpack, starterGear) -- Sharing variables between this script and all item modules scripts.

    ItemModule.BackpackItems = {

        ["BackpackSample"] = backpack:FindFirstChild("Sample"),
        ["StarterSample"] = starterGear:FindFirstChild("Sample")

    }

    local item = ItemModule.BackpackItems

    if (item["BackpackSample"] ~= nil) or (item["StarterSample"] ~= nil) then
        print("item already exists!") -- this prints out, but the return doesn't work.
        return
    end
end



return ItemModule

Item giver script

local ItemModule = {}
local module = require(game.ServerStorage.ItemTable)
local itemFolder = game.ServerStorage.ItemStorage
local item = itemFolder.Sample -- Chosen item, makes creating other scripts easier. (because it uses this single variable!)

function ItemModule.itemFunc(player)
    local backpack = player.Backpack
    local starterGear = player.StarterGear
    local check = false

    module.inventoryData(backpack, starterGear)  -- inventorydata function runs here.

    local itemClone = item:Clone()
    local itemCloneBackup = item:Clone()

    itemClone.Parent = player.Backpack -- Clones to player's backpack.
    itemCloneBackup.Parent = player.StarterGear -- Clones to player's StarterGear (so if they die, they'll still have it).
end

return ItemModule

1 answer

Log in to vote
0
Answered by
Raccoonyz 1092 Donator Moderation Voter
4 years ago

I think your confused on what return does. Return returns a value for the current function. It doesn't stop all layers of functions - only the one it is on.

Here is an example.

function exampleFunction()
    print("Hello universe!")
    return
end
print("Hello world!")
exampleFunction()
print("Hello galaxy!") -- This will print

You can fix this by making the return value false. Here is an example.

Table script:

local ItemModule = {}
local itemFolder = game.ServerStorage.ItemStorage

ItemModule.ItemStorage = { -- For cloning into the players backpacks in item module's scripts.

    ["Sample"] = itemFolder.Sample

}

function ItemModule.inventoryData(backpack, starterGear) -- Sharing variables between this script and all item modules scripts.

    ItemModule.BackpackItems = {

        ["BackpackSample"] = backpack:FindFirstChild("Sample"),
        ["StarterSample"] = starterGear:FindFirstChild("Sample")

    }

    local item = ItemModule.BackpackItems

    if (item["BackpackSample"] ~= nil) or (item["StarterSample"] ~= nil) then
        print("item already exists!") -- this prints out, but the return doesn't work.
        return false
    end
end



return ItemModule

Item giver script:

local ItemModule = {}
local module = require(game.ServerStorage.ItemTable)
local itemFolder = game.ServerStorage.ItemStorage
local item = itemFolder.Sample -- Chosen item, makes creating other scripts easier. (because it uses this single variable!)

function ItemModule.itemFunc(player)
    local backpack = player.Backpack
    local starterGear = player.StarterGear
    local check = false

    local doReturn = module.inventoryData(backpack, starterGear)  -- inventorydata function runs here.
    if not doReturn then return end

    local itemClone = item:Clone()
    local itemCloneBackup = item:Clone()

    itemClone.Parent = player.Backpack -- Clones to player's backpack.
    itemCloneBackup.Parent = player.StarterGear -- Clones to player's StarterGear (so if they die, they'll still have it).
end

return ItemModule
0
Thanks! I figured out a way to solve it about 2 minutes ago but I'll use your solution since it's a better method. I appreciate the explanation on returns, I never really understood them before. sidewinder44 20 — 4y
0
Sadly it didn't work so I'll just use the solution I found, by moving the cloning over to the table script. sidewinder44 20 — 4y
Ad

Answer this question