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

Code redeemable after player rejoins even though they redeemed it already?

Asked by 3 years ago

Hi, Im trying to make a feature in my game where you can enter a code and it will give you points. All of that works, but if the player has redeemed the code and rejoins, they can redeem it again, so they can keep getting points. I know this has something to do with datastores, but I barely know anything about them lol. Anyways, I would really appreciate some help. Thanks!

Here is the script in serverscriptservice which gets activated once a remote event gets fired, which is when they redeem the code

01local remoteevent = game.ReplicatedStorage.CodeEvent
02 
03remoteevent.OnServerEvent:Connect(function(Player,reward,code)
04    if Player:FindFirstChild(code) == nil then
05        local redeemed = Instance.new("BoolValue", Player)
06        redeemed.Name = code
07        redeemed.Value = false
08 
09        if redeemed.Value == false then
10            Player.leaderstats.Points.Value += reward
11            redeemed.Value = true
12        end
13    end
14end)
0
@DriBowser I have edited my answer. I forgot to add the GetDataStore function. The code should work now. MarkedTomato 810 — 3y
0
Oh, thank you very much, I'll test it now. DriBowser 55 — 3y

1 answer

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

Problem

Yes, we need to use DataStoreService.

Solution

We don't need to create the BoolValue Instance, we can simply store a bool value in DataStores.

When using Data Stores you always need to use a pcall AKA protected call because Roblox DataStores can fail sometimes.

Fix

01local DataStoreService = game:GetService("DataStoreService")
02 
03 
04local DataStore_CodeName = DataStoreService:GetDataStore("RedeemCode", "CodeName")
05local DataStore_CodeName2 = DataStoreService:GetDataStore("RedeemCode", "CodeName2")
06 
07local CodeRedeemed = game.ReplicatedStorage.CodeEvent
08 
09 
10local function OnCodeRedeemed(Client, Code)
11    local Success, Result
12 
13 
14    if Code == "CodeName" then
15 
View all 58 lines...

EXPLOITING PROBLEM

I've found a way to easily exploit your game.

I see that you passed a Reward argument through the RemoteEvent. So I can exploit it with an executor and do this

1game.ReplicatedStorage.CodeEvent:FireServer(1000000, "CodeName")

And there we go, I just gave myself 1M points. So how do we fix this? Just don't pass stuff like that to the server and do the rewarding on the server.

You can understand DataStores here. It explains everything about DataStoreService and how you can use it. BTW, you don't need to read the whole article all at once. You can read bit by bit otherwise, you'll get overwhelmed. If there's any problems with the code, you can reply to me in the comments on my answer. You should play around with DataStores too, so you can understand and feel comfortable using them.

0
Thank you so much for helping me, but I'm getting an error with the 4th line in the code saying "attempt to call an instance value" DriBowser 55 — 3y
0
Yep, it works now, thank you so much! DriBowser 55 — 3y
Ad

Answer this question