Idiot fails at making datastore

I am not very good at scripting DataStores, and I already failed. I am trying to save a string to a datastore, how can i manage?

--// Services
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")

--// Variables
local DS = DSS:GetOrderedDataStore("Data7")

--// Function

local function OnPlayerAdded(player)
	local plr_key = "id_"..player.UserId
	local plr_key2 = "2id_"..player.UserId
	local success, data = pcall(function()
		return DS:GetAsync(plr_key)
	end)
	local success, data2 = pcall(function()
		return DS:GetAsync(plr_key2)
	end)
	if data2 == nil then
		DS:SetAsync(plr_key2, "")
	end
	
	local stats = Instance.new("Folder")
	stats.Parent = player
	stats.Name = "leaderstats"
	
	local AeroCoins = Instance.new("IntValue")
	AeroCoins.Parent = stats
	AeroCoins.Name = "AeroCoins"
	game.ReplicatedStorage.RewardEntity.OnServerEvent:Connect(function(Player, EntityName)
		local success, result = pcall(function()
			data2 = data2 .. EntityName .. ","
			return DS:SetAsync(plr_key2, data2)
		end)
	end)
	game.ReplicatedStorage.RewardCash.OnServerEvent:Connect(function(Player, Player2, Reward)
		if Player == player then
			AeroCoins.Value += Reward
		end
	end)
	if success then
		AeroCoins.Value = data or 0
	end
	game.ReplicatedStorage.Values.DoorNumber.Changed:Connect(function(Value)
		AeroCoins.Value += math.random(1, 4)
	end)
end

local function OnPlayerRemoving(player)
	local plr_key = "id_"..player.UserId
	local success, result = pcall(function()
		return DS:SetAsync(plr_key, player.leaderstats.AeroCoins.Value)
	end)
	if not success then
		warn(result)
	end
end

local function OnServerShutdown()
	for _, player in Players:GetPlayers() do
		local plr_key = "id_"..player.UserId
		local success, result = pcall(function()
			return DS:SetAsync(plr_key, player.leaderstats.AeroCoins.Value)
		end)
		if not success then
			warn(result)
		end
	end
end

--// Connections
Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(OnServerShutdown)

--// AeroCoin Payment

1 Like
DS:SetAsync(plr_key2, "")

The above line of code is unnecessary anyway. Remove it

2 Likes

It is necessary. I need to create an empty string, otherwise some other code won’t work.
Also, thank you! You solved my problem. I forgot about the difference of datastore types!

1 Like

Well you’re using the data-store inappropriately then. Eliminate this necessity or find a way to use numbers. You might have to use a separate data-store altogether. You have additional writes to the data-store which are not pure integers, so you’ll need to resolve those issues. You’re going to cause data corruption/mis-matching and exhaust the API by having embedded event listeners to RewardEntity and RewardCash inside of Players.PlayerAdded. Handle those events individually.

On another note, by the very nature of the RewardCash event, you’ve invited an infinite cash vulnerability into your game. The client should never be able to tell the server how much cash they should be rewarded and when

1 Like

Explain it to a coding idiot. Because I am not too good at understanding the reply above.

1 Like