Reading information from within a function in the rest of the script

Hello, I’m trying to figure out how I can read what information is within a function and transfer that to the rest of the script outside the function. In the image attached, I have a script that detects when a player joins and prints two character values, which I want to run certain functions depending on what value the player has.
image

If anybody knows how I could do that effectively then please let me know.
Thank you if you took the time to read this, and I appreciate any and all help.

1 Like

what i would do (not saying its the best way but you know), is make a table outside of the function with global variables, and then insert the player’s values when joining. for example:

local AbilityLoad = game.ReplicatedStore.Remotes.AbilityLoad

local PlayerAbilities = {}

game.Players.PlayerAdded:Connect(function(player)
    PlayerAbilities[player.Name] = {
        AbilityVal = player:WaitForChild("Stats").Ability.Value,
        SubAbilityVal = player:WaitForChild("Stats").Ability.Value
    }
    print(PlayerAbilities)
    --[[ 
    gives something like this
    {
         ["Dane1up"] =  ▼  {
             ["AbilityVal"] = 546, -- random value
             ["SubAbilityVal"] = 221 -- random value
        }
    }
]]--
end)

This allows for you to get the player’s info outside the function by using PlayerAbilities[player.Name] in a table, as well as save multiple player’s data in one singular script

then when the player leaves i would simply set their value in the PlayerAbilities table to nil. let me know if that solves or points you in the right direction!

1 Like

I’m doing this script via server side, should I make it a local, and would this have any crossfire if lets say several people join at once? Since what I want to do is the system detects “OK this person has so and so ability, attach this model to them and give them these animations”, so I wouldn’t want the system to give the abilities to the wrong people, does that make sense?

Why not just run the values when the player gets added?

you can do that from the server, but theres no need to put those values global if thats the case. make a global function that takes in the values that you want and then send those values in when the player joins to attach models. could be something like this

local function AttachPlayerModels(Player, Ability1, Ability2)
    if Ability1 == "random ability" then
        -- attach models and give player animations
    end
end

game.Players.PlayerAdded:Connect(function(player)
    local AbilityVal = player:WaitForChild("Stats").Ability.Value
    local SubAbilityVal = player:WaitForChild("Stats").Ability.Value

    AttachPlayerModels(player, AbilityVal, SubAbilityVal)
end)