Getting variables from other functions?

Hey! I’m looking for a way to get variables from other functions – without using ‘return’, because in my circumstance this wouldn’t work. The issue comes when I’m trying to add stats to a player with it’s function being it’s own. I’ve tried using some things I doubted myself would work, and I’ve searched the devfourm – and didn’t find anything to answer my problem.

The module is required by one core script.

local PlayerManager = require(ServerModules.PlayerManager)

Players.PlayerAdded:Connect(function(Player)
	PlayerManager:RunServer(Player)
end)

Here’s the code:

local M = {}

function M:IncreaseValue()
-- I want to gather the value from the RunServer function.
end

function M:RunServer(Player)
	local StatsFolder = script.PlayerInfo
	local PlayerInfo = StatsFolder:Clone()
	local PlayerStats = PlayerInfo.Stats
	local Hunger, Energy, Health = PlayerStats.Hunger, PlayerStats.Energy, PlayerStats.Health
	PlayerInfo.Parent = Player
	local Role, Rank = PlayerInfo.Role, PlayerInfo.Rank
	Role.Value = tostring(Player:GetRoleInGroup(GROUPID))
	Rank.Value = Player:GetRankInGroup(GROUPID)
end

return M
1 Like

Couldn’t you just create a table inside of the module that contains each player’s values, then increment or decrement it as normal? I’m not really sure what you’re trying to access from the other function but try something like this:

local M = {}

M.PlayerStats = {}

function m:IncreaseValue(player,stat,value)
    M.PlayerStats[player][stat] += value
end

function M:RunServer(player)
    local statsFolder = script.PlayerInfo
    local playerInfo = statsFolder:Clone()
    local playerStats = playerInfo.Stats
    self.PlayerStats[player] = {playerStats.Hunger; playerStats.Energy; playerStats.Health;}
    playerInfo.Parent = player
    local role, rank = playerInfo.Role, playerInfo.Rank
    role.Value = player:GetRoleInGroup(GROUPID)
    rank.Value = player:GetRankInGroup(GROUPID)
end

return M
1 Like