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
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)
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.