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

How do I perform different math functions depending on a variable?

Asked by 4 years ago
Edited 4 years ago

I'm trying to create a function that checks the value of a playerstat and compares it to a given value.

For example if a player tries to buy something it should check if the playerstat (money) is more than or equal to the given value (the price of an item).

However there are other situations where I have to check if the playerstat is either greater than or lesser than the given value.

I'm having trouble figuring out how to tell the script what math comparison to do. I tried storing math operators such as > and == in a table or variable but found out you can't store those.

For example I can't do (as a watered down version)

equalTo = ==
lessThan = <

function blah(operator, arg1, arg2)
    if arg1 operator arg2 then
        print(true)
    end
end

blah(equalTo, 5, 5)

I have done a lot of searching but feel like I don't know the correct lua terms to find what I'm looking for if it is out there. Any help appreciated, thanks!

edit: I made it work with the biggest hackjob of if statements for testing sake but surely there's a better way

local operators = {"==", ">", "<", ">=", "<="}

function PlayerModule.CheckData(player, dataName, value, numerator)
    data = playerData[player]
    for i, dataValue in pairs(data) do
        if i == dataName then
            for x, y in pairs(operators) do
                if y == numerator then
                    if x == 1 then
                        if dataValue == value then
                            return true
                        end
                    elseif x == 2 then
                        if dataValue > value then
                            return true
                        end
                    elseif x == 3 then
                        if dataValue < value then
                            return true
                        end
                    elseif x == 4 then
                        if dataValue >= value then
                            return true
                        end
                    elseif x == 5 then
                        if dataValue <= value then
                            return true
                        end
                    end
                end
            end 
        end
    end
end

0
Why can't you just directly use == or <, I mean you can just make different functions using different operators User#24403 69 — 4y
0
I just wanted it to be cleaner and have one function for all stat comparisons. Deputy_McNugget 14 — 4y
1
A one-does-all doesn't make code cleaner. If anything, it makes it messier because it's harder to pin point what something is actually doing and what it's true purpose is. You would be much better off just comparing your data using the operators when you need them. You'd be doing the same thing with this function anyway. ScriptGuider 5640 — 4y
1
Hmm okay, I've only been doing this a few days and thought I could get away with this in an easy way. Thanks guys! Deputy_McNugget 14 — 4y
View all comments (2 more)
0
Well there are few things you could change. For example, I've seen you're comparing things basically twice. If you're using >=, dont compare it again with >. It would return true if its equal to, or greater than the given value. Meaning, it treats those as separate conditions. (Basically saying: if num > num or num == num then) . Hopefully that it least helps you simplify it. Psudar 882 — 4y
0
If Player balance >= Store Price then *copy item to player inventory and subtract balance* else print("the required amount was insufficient") BlackeyeI 19 — 4y

Answer this question