Hello, I'm a novice at coding and I've been working on an rpg battle system that makes it so whenever a player touches an enemy NPC, The Player who initiates the Battle is warped to a room while the NPC is temporarily replaced with a Model that is able to warp other players to join in on the battle.
The Part I'am stuck on is figuring out how to make it so that only 4 players at a time can participate in a battle, I've seen various tutorials such as using Tables and Region3 that i thought would help me accomplish this but i'm having hard time trying to figure out how I should go about do this.
Number values. They are amazing. For example, using a number value you could store how many players are in the fight. Then, if another player tries to join, the script could check to see how many people are in the fight. If it's too much, the script won't allow the player to join. Here's the documentation: https://developer.roblox.com/en-us/api-reference/class/NumberValue
I'd probably just use a table and use that to set a max count. For example:
This code assumes that you're using a touched event to teleport players.
local MAX_COUNT = 4 local PlayersBattling = {} local debounce = false Part.Touched:Connect(function(Hit) if debounce then return end debounce = true if #PlayersBattling > 4 then return end local Humanoid = Hit.Parent:FindFirstChild("Humanoid") or Hit.Parent.Parent:FindFirstChild("Humanoid") or nil if Humanoid then PlayersBattling[#PlayersBattling + 1] = game.Players:GetPlayerFromCharacter(Humanoid.Parent) --Code to teleport player end debounce = false end)
Make sure to remove players from the PlayersBattling table when they are done battling.