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)!
1 comment
I believe I have fixed the answer, sorry for misinterpreting this.