Im trying to create a racing system where one player can choose players inside their server(the chosen players are given an option to accept or decline) to race with, to do this I thought it would be a good idea to put all the chosen players inside an array so I can access a certain player incase someone declines the request, then tell the other people in the array that they declined, I want to show them the name of the person that declined instead of just saying someone declined. My issue is that I can’t access the array from multiple(local) places, I tried using a module script but if I add values to the array in 1 local script, then accessing it from another, the values aren’t there. Even If I am able to do that, how would I make it so if multiple players decide to use this race feature at the same time, it wouldn’t get messed up. Or is there a better way to do this race system in general?
When you say you like the idea, do you mean the idea of putting the players into an array? Also, what would I do if multiple people create a race at the same time? If I use remotes to transfer the data from script to script, and if another person made a race, wouldn’t it mess up the array?
I like the idea for the race system in general, and yes, the array too!
Actually, you may want multiple arrays for this.
One that stores the players which have initiated a race challenge, and who they have challenged. I would use the player UserId for the key.
In the the other you would store somewhat of the opposite. Each challenged player would receive the challenger in thier table. Each new challenger would be added to the end of the table.
So you’d have two tables, indexed by the player UserIds.
Two tables of tables actually.
Good thing we like tables here.
In code, maybe something like this:
local Players = game:GetService("Players")
local RaceMakers = {}
local ChallengedRacers = {}
local function NewRace(player, challengedPlayers)
--make a fresh table for the player
RaceMakers[player.UserId] = {}
--we want to verify challengedPlayers, since it came from a remoteEvent
if type(challengedPlayers) == "table" then
for _,chalplayer in ipairs(Players:GetChildren())do
--find the challengedplayers userId on the server
if table.find(challengedPlayers, chalplayer.UserId) then
--add challenged player to racemakers table
table.insert(RaceMakers[player.UserId], chalplayer.UserId)
--locate or create a table for the challenged player
ChallengedRacers[chalplayer.UserId] = ChallengedRacers[chalplayer.UserId] or {}
--add the player who challenged to thier table
table.insert(ChallengedRacers[chalplayer.UserId], player.UserId)
end
end
end
end
I appreciate the reply, however I don’t fully understand what you’re saying, also what if the person that makes the race chooses multiple people to race against? I know i didn’t say that previously but I want a race to include any number of people.
Sorry, I tried to break it down the best I could. You’re working on a complicated system.
The example code I provided (ServerScript) - would receive an array of user Ids, the “challengedPlayers”.
So, in your remoteEvent in the clients LocalScript,
Where you’ve configured it so send an array of userIds that look like this:
your player would do
Remote:FireServer({384528347,2398462364,182376213,127356123}) --all the challenged players userIds
And the server would handle it with the other code.
This is like, bare-bones basic example though… You’re table data would likely need to be more complex.
Good luck!