Comparing a Variable to another

The piece of code I attached currently doesn’t work, I have figured out why through printing values I have carried from different module scripts.

The error lies in statements like the following -

if strval == inputtable[1] then

rarity = "A"

end

The problem is that I don’t know why I can’t write code like this and why the system can’t check if strval and inputtable[whatever number] are the same, is it just not allowed or is there a problem with my code?

Edit: The error lies in the fact the variable rarity is being returned as nil as it is meant to give a capital letter as a string value depending on what strval is entered

Thanks for your help in advance.

2 Likes

Do you mean it doesnt require from another script?

It does require; I call the function in a server script and it returns ‘rarity’ as a nil value. In the screenshot, I include a part of the output box that shows the values being returned, the first one that reads nil is what rarity returns

This is an image of my server script.

Edit my bad i realise I did not specify the problem that well in the post

1 Like

Can you scroll down on module script I am gonna figure this out.

1 Like

Thanks so much! There is nothing else further down on the module script that I attached but I can attach the other module scripts I used if you want.

1 Like

Can you try this script it might work?

local Inputtable = {
    "a",
    "b",
    "c"
}

local Ascorrespodent = {
    "A"
}

function maps2cluefolder.dmfm(strval)
    local rarity

    -- Case-insensitive comparison
    local function caseInsensitiveCompare(a, b)
        return string.lower(a) == string.lower(b)
    end

    if caseInsensitiveCompare(strval, Inputtable[1]) then
        rarity = "A"
    elseif caseInsensitiveCompare(strval, Inputtable[2]) then
        rarity = "B"
    elseif caseInsensitiveCompare(strval, Inputtable[3]) then
        rarity = "C"
    end

    print("strval:", strval)
    print("Inputtable[1]:", Inputtable[1])
    print("rarity:", rarity)

    return rarity
end

-- try this script ---

3 Likes

Sorry for the halfing Idk what to do.

1 Like

Ah, this didn’t work but it did lead me to my error which had to do with my first function return not being a string value… thank you for your help as my actual issue lies in specific coding which I will have to learn as i get better.

Edit: I just changed return pf to return pf.Name

2 Likes

Sorry for the error try this instead

local map2map = {}

function map2map.mrcp(folder)
    local children = folder:GetChildren()

    if #children > 0 then
        local randomChildIndex = math.random(1, #children)
        local randomChild = children[randomChildIndex]
        return randomChild.Name
    else
        return nil  -- Handle the case where the folder has no children
    end
end

return map2map

2 Likes

oh no your code worked fine after I changed pf to a string value and not an instance

3 Likes

Oh alright, I hope you got a good day Hppy coding!

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.