How would i structure a character selections + abilities

I had an idea of making a character selection with custom abilities based on the character and stuff and when i thought of it i came up with this:

My Idea:

when the player joins the game they get a gui with options of the characters. And based on the one they click it changes a string value’s value to the name of the chosen character

local CharValue = --value in the player
local UIS = game:GetService("UserInputService)

UIS.InputBegan:Connect(function()
   if CharValue = Character1 then
   --Ability one
   end
end)
local CharValue = --value in the player again
local UIS = game:GetService("UserInputService)

UIS.InputBegan:Connect(function()
   if CharValue = Character2 then
   --Ability one
   end
end)

The problem is when i add many characters many scripts will fire at once doing that check which i dont know if its practical.

2 Likes

bumping bc no one saw this yet

1 Like

Loop through a table for characters and their abilites, dont make individual scripts. And to prevent them from firing all at once, you can utilise :Disconnect() or some limiting system

2 Likes

Make the GUI with the character selections, then place them into StarterGUI. Once the player selects the character they want, the GUI will get disabled, and a remote event that fires the server needs to be fired. On the server, there needs to be a PlayerAdded script that adds the string value to the player. Once the server gets fired, it should change the string value’s value based on the character that was passed as a parameter by the client. You do not need UserInputService, just MouseButton1Click for the GUI with character selections

1 Like

Yes i know this, read the rest to know what the problem is.

1 Like

Im confused, can you explain a bit more on how i would use a table?

1 Like

Couldn’t you handle all characters with one script?

1 Like

You could but its gonna be a big pain and will cause sphagetti code and i will still need the if statements to check which character the player chose which doesnt solve my problem and makes it worse

Yes I can, I’ll be home soon on my pc

1 Like

Alr im home, just to clarify, what is CharValue, a string, a number?

It is a string value, the value changes when the player chooses a character to the characters name.

local CharValue -- whatever the value is
local UIS = game:GetService("UserInputService")

local Characters = {
    Character1 = { -- char 1, you can rename the index to whatever
        Ability1 = function()
            --Ability one
            -- make it do whatever here, for example increase speed
        end,
        Ability2 = function()
            --Ability one
        end,
    },
    Character2 = {
        Ability1 = function()
            --Ability one
        end,
    }
}

UIS.InputBegan:Connect(function()
    for Character, Functions in Characters do
        if CharValue == Character then
            for _, AbilityFunction in Functions do
                AbilityFunction() -- to activate the ability
            end
        end
    end
end)

Here is a brief small example of what I mean, of course this can be changed up and edited as it is an example. Something like this will prevent you from making many scripts and more unnecessary connections

2 Likes

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