Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make it so that only a certain number of players can participate?

Asked by
138mic 0
4 years ago
Edited 4 years ago

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.

2 answers

Log in to vote
0
Answered by
Raccoonyz 1092 Donator Moderation Voter
4 years ago
Edited 4 years ago

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

0
Interesting...So Im not 100% following on how NumberValues work exactly so please forgive me, but is the idea that I have it so that for every person who joins, I have to have it so the game stores them inside of a NumberValue until it reaches 4? 138mic 0 — 4y
0
Yes. Number values have a value inside of them which can be edited. Raccoonyz 1092 — 4y
0
For example, you could do game.Workspace.NumberValue.Value = "1" and then the value in the number value would be able to be accessed at any time by any scripts Raccoonyz 1092 — 4y
0
Okay so want to make sure i'm on the right track with this, if i were to write: part:MoveTo(Position1.Position2) and follow up with game.Workspace.BattleRoomMax.Value = "1", when the player warps to the battle room, the ' BattleRoomMax.Value = "1" ' line stores the player who warped into the value?  138mic 0 — 4y
View all comments (2 more)
0
No. First off, you need to make a new number value for each battle. Secondly, you need to use 2 equal signs to change the value. Friend me on Discord at Raccoonyz#0001 if you want better help. Raccoonyz 1092 — 4y
0
Nevermind, you don't need 2 equal signs Raccoonyz 1092 — 4y
Ad
Log in to vote
0
Answered by
palav 104
4 years ago
Edited 4 years ago

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.

Answer this question