Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to only allow certain amount of people to do things?

-- This is on a local script on the textbox
local player = game.Players.LocalPlayer
if script.Parent.Text == "Only100Times" then
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 100
end

I want to only allow 100 players in from all of the server to get the reward if from all of the server people have 100 people used the code "Only100Times" then i want to make this code expired but i don't know how to do that.

1 comment

0

I believe I have fixed the answer, sorry for misinterpreting this.

1 answer

✓ Accepted answer

First, insert a RemoteEvent in ReplicatedStorage and name it "CodeUsed". Next, insert a Script in ServerScriptService and use the below text (EDIT: I'm now implementing the usage of data store to not only keep track of who has used the code, but also keep more than 100 people form using the code across all servers):

local DSS = Game:GetService("DataStoreService")
local LimitedCodeStore = DSS:GetDataStore("LimitedCodeStore")
local LimitedCodeUsed = DSS:GetDataStore("LimitedCodeUsed")

local codeUsed = game.ReplicatedStorage:WaitForChild("CodeUsed")

codeUsed.OnServerEvent:Connect(function(plr, codeText)
	if codeText == "Only100Times" and  not LimitedCodeStore:GetAsync(plr.UserId) then
		LimitedCodeStore:SetAsync(plr.UserId, true)
		local timesUsed = LimitedCodeUsed:GetAsync("Only100Times")
		if timesUsed < 100 then
			LimitedCodeUsed:SetAsync("Only100Times", timesUsed + 1)
			plr.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 100
		elseif timesUsed == nil then
			LimitedCodeUsed:SetAsync("Only100Times", 1)
			plr.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 100
		end
	end
end)

Alright, to unpack what we did in that script, we connected the remote event to a function that will keep track of how many times the code has been used. This function will also check if that number is too high, and if its not then it will reward the player. You may noticed that I added coins in the server script. This is because the way that you're currently doing it (in the local script) will not replicate to the server and therefore the player actually gained nothing. Lets fix your local script and fire the event as well:

local codeUsed = game.ReplicatedStorage:WaitForChild("CodeUsed")

if script.Parent.Text == "Only100Times" then
	codeUsed:FireServer("Only100Times")
end

Thats all that you need to do. Just to summarize everything that is happening, heres a step by step process:

1.) If the code being used is "Only100Times", the "CodeUsed" event will fire and bring the player as a variable by default, but also the text you want.

2.) The server script will start running because it received the event

3.) The server will check if the stored amount of times used is less than 100

4.) If this is true, the player will receive 100 coins and if the uses reaches 100, the code can no longer be used.

I hope I helped and leave a comment if you have any questions or this turns out to have an error (I don't currently have access to studio, so this may be the case)!

5 comments

0

I think he wants the code to be disabled in the whole game, not just the server

0

I want it to be for all server like if one server someone put the code we +1 and when it reaches 100 or whatever it will stop for all servers.

0

I think this is a reasonable answer (though evidently not was mastercheeseit wanted), but note that the above scripts do not prevent someone from submitting the same code multiple times.

0

To get what mastercheeseit wants, data stores will be required. To use the code, the server must call UpdateAsync on some data store (unlike most data store calls this will be to a shared key, not based on the player's user id). The content of the data store key could be the list of player ids of the players who have submitted the code.

0

In this way you can make sure that someone hasn't already submitted the code and also stop more than 100 people from gaining 100 coins.

This discussion is preserved as it was. New answers, comments, and votes are closed.