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

How do i reference a functions outcome if its a string?

Asked by 4 years ago

Hello, Im trying to spawn in a certain part at a certain location using the string of a random rarity generator i made. I just cant seem to figure out how to reference the string as a Boolean value.

local Spawner = script.Parent
local ServerStorage = game:GetService("ServerStorage")
local AridPlants = ServerStorage:FindFirstChild("AridPlants")

local Rarity = {
                {"Common",40},
                {"UnCommon",35},
                {"Rare",20},
                {"Legendary",4},
                {"Mythic",1}
}
local TotalWeight = 0

for _,ItemData in pairs(Rarity) do
    TotalWeight = TotalWeight + ItemData[2]
end

local function chooseRandomRarity()
    local chance = math.random(1,TotalWeight)
    local counter = 0
    for _,ItemData in pairs(Rarity) do
        counter = counter + ItemData[2]
        if chance <= counter then
            return ItemData[1]
        end
    end
end
    while true do
        Spawner.ClickDetector.MouseClick:Wait()
            print(chooseRandomRarity())
    end

        if chooseRandomRarity() == "Common" then
            local c = AridPlants:FindFirstChild("1")
            c.Parent = game.Workspace.PlantSpawner
            c:MoveTo(Spawner.Position)
            print("Spawned")

        end
0
You mean return something on a function if its a string? If it is, use if typeof(variable) == "string" then Dfzoz 489 — 4y

1 answer

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

Dfzoz is right, if you want to check if the outcome is a string you would check the type of the value.

Type VS TypeOf

Why TypeOf Is Better


For example you can use type()

print(type("Hello World"))

RESULT: string

If you want to check if it is a certain type you can use an If then statement.

if type(UDim2) == "table" then
    print("Table")
end

RESULT: Table


Another way to do this is to use 'typeof()'

print(typeof(UDim2))

RESULT: table

If you want to check if it is a certain type you can use an If then statement

if typeof("Hello World") == "string" then
    print("This is a "..string.upper(typeof("Hello World")))
end

RESULT: This is a STRING


Hope this helped! Dfzoz was the first one to comment this, I just expanded on his comment.

If this helped you with your question, don't forget to select it as an answer!

Ad

Answer this question