How would I add a DataStore to this script?

So I have created a codes GUI with working script and stuff but the one problem is that it doesn’t DataSave and I don’t understand where to put the DataSave and how to incorporate it without breaking anything.

LocalScript Child to Frame

local Codes = {"Unknown1","Unknown2","Unknown3"}
local RemoteEvent = game.ReplicatedStorage.CodeEvent

script.Parent.EnterButton.MouseButton1Click:Connect(function()
	if script.Parent.InputBox.Text == Codes[1] then
		RemoteEvent:FireServer(100,Codes[1])
	else
		if script.Parent.InputBox.Text == Codes[2] then
			RemoteEvent:FireServer(200,Codes[2])
		else
			if script.Parent.InputBox.Text == Codes[3] then
				RemoteEvent:FireServer(300,Codes[3])
			end
		end
	end
end)

Script Child To SeverScriptStorage

local RemoteEvent = game.ReplicatedStorage.CodeEvent

RemoteEvent.OnServerEvent:Connect(function(Player, Reward, Code)
	if Player:FindFirstChild(Code) == nil then
		local Redeemed = Instance.new("BoolValue", Player)
		
		Redeemed.Name = Code
		Redeemed.Value = false
		
		if Redeemed.Value == false then
			Player.leaderstats.Coins.Value += Reward
			Redeemed.Value = true
		end
	end
end)

My DataStore Script

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	local plrkey = "id_"..plr.UserId
	local save1 = plr:WaitForChild("leaderstats").Wins
	local save2 = plr:WaitForChild("leaderstats").Coins

	local data
	local success, whoops = pcall(function()
		data = ds:GetAsync(plrkey)
	end)

	print(data)
	if success and data then
		save1.Value = data.Wins
		save2.Value = data.Coins
		print("Loaded data to: ", plr.Name)
	else
		warn("This user has no data or an error has occured, more info: ", whoops)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local data = {
		Coins = plr.leaderstats.Coins.Value,
		Wins = plr.leaderstats.Wins.Value
	}
	local success, whoops = pcall(function()
		ds:SetAsync("id_"..plr.userId, data)
	end)

	if success then
		print("Data saved successfully!")
	else
		warn("An error occurred saving the data, more info: ", whoops)
	end
end)

Side topic I see a vulnerability, you are sending player reward and checking a table of correct codes via local, which an exploiter can see. An exploiter could do this

RemoteEvent:FireServer(9999999999999999999,"whatevercodestringintable")

consider just sending what the player inputs, to the server then check if its a valid code from there and reward accordingly

Inside of the data table in PlayerRemoving, you could add Redeemed = Player.Redeemed.Value

Then, inside of the PlayerAdded, when you set the value of the wins and coins, you would set the Value of redeemed to the value inside of the table.

Reply to me if I’m wrong, I don’t really work with datastores often

1 Like

How would I do that then?

I dont want exploiters hacking

I get this

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	local plrkey = "id_"..plr.UserId
	local save1 = plr:WaitForChild("leaderstats").Wins
	local save2 = plr:WaitForChild("leaderstats").Coins
	local save3 = plr.Redeemed.Value

	local data
	local success, whoops = pcall(function()
		data = ds:GetAsync(plrkey)
	end)

	print(data)
	if success and data then
		save1.Value = data.Wins
		save2.Value = data.Coins
		print("Loaded data to: ", plr.Name)
	else
		warn("This user has no data or an error has occured, more info: ", whoops)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local data = {
		Coins = plr.leaderstats.Coins.Value,
		Wins = plr.leaderstats.Wins.Value,
		Redeemed = plr.Redeemed.Value
	}
	local success, whoops = pcall(function()
		ds:SetAsync("id_"..plr.userId, data)
	end)

	if success then
		print("Data saved successfully!")
	else
		warn("An error occurred saving the data, more info: ", whoops)
	end
end)

just have the checks, on the server script instead of the local.

fire to the server, the textbox text. only the text, then have the server deal with checks.

Ok show me how you would do that?

script.Parent.EnterButton.MouseButton1Click:Connect(function()
		RemoteEvent:FireServer(script.Parent.InputBox.Text)
end)

By Redeemed I meant whatever the name your IntValue was that checks if the player has redeemed it or not. Based on your script I’m assuming you have multiple IntValues which are named exactly what the code is that are created when the code is entered, so you’d have to use FindFirstChild on the IntValue to make sure it exists.

Can you maybe put that in the script I don’t understand this.

Does anyone have a idea how???

Acually nvm it is in the player.

SInce I put Code here would that mean its name is code and not redeemed??