So I’m just wondering how I would go about changing a function’s expected parameter type depending on some other parameter?
In my example I’m working with a folder of different values which relate to certain round stats (eg. round time, round status etc), all of which are different types of value bases. So, suppose you go about approaching it by creating a module to set these values instead of just setting it manually (works better with my workflow):
local module = {}
function module:SetRoundStat(statName: 'RoundStatus' | 'TimeRemaining', value: any)
gameStats:FindFirstChild(statName).Value = value
end
Okay, so that’s what I’m fine with thus far. However, as “RoundStatus” is a stringvalue, and “TimeRemaining” is an intvalue, I would like to have the value parameter of my function to change to either string or number depending on whether statName is “RoundStatus” or “TimeRemaing”, instead of just “any”. Is this possible?
local module = {}
function module:SetRoundStat(statName: 'RoundStatus' | 'TimeRemaining', value: any)
local stuff_to_change = gameStats:FindFirstChild(statName)
if not stuff_to_change then return end
if stuff_to_change.Name == 'RoundStatus' then
value = value:tostring()
elseif stuff_to_change.Name == 'TimeRemaining' then
value = value:tonumber()
end
stuff_to_change .Value = value
end
I mean when I go to call the function from another script, the expected type of ‘value’ would change depending on what statName is.
Using your method, it still defaults back to any.
function module:SetStat(stat: 'RoundStatus' | 'TimeRemaining', value: any)
local stuff_to_change = gameStats:FindFirstChild(stat)
if stuff_to_change.Name == 'TimeRemaining' then
value = tonumber(value) :: number
end
stuff_to_change.Value = value
return module
end