Help with datastore

So I am making a profile system where one user can leave a rating on another user’s profile. Currently my datastore is looking like this:

local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
local Players = game:GetService("Players")


local onJoin = function(player)
local getData = Datastore:GetAsync(player.UserId)
if getData[1] then
	player:SetAttribute("AverageRating", getData[1])
	player:SetAttribute("RatingsReceived", getData[2])
	player:SetAttribute("RatingsGiven", getData[3])
end
end

local onLeave = function(player)
local data = {}
table.insert(data, player:GetAttribute("AverageRating"))
table.insert(data, player:GetAttribute("RatingsReceived"))
table.insert(data, player:GetAttribute("RatingsGiven"))

Datastore:SetAsync(player.UserId, data)
end


Players.PlayerRemoving:Connect(onJoin)
Players.PlayerAdded:Connect(onJoin)

while task.wait(120) do
local allPlayers = Players:GetPlayers()


for _,player in pairs(allPlayers) do
	local data = {}
	table.insert(data, player:GetAttribute("AverageRating"))
	table.insert(data, player:GetAttribute("RatingsReceived"))
	table.insert(data, player:GetAttribute("RatingsGiven"))
	
	Datastore:SetAsync(player.UserId, data)
end
end

The way I have it set up is so every time someone leaves a rating on someone else’s profile, it takes that rating, adds it to their current average rating, divides by two, and sets that as their new “AverageRating” attribute. Every two minutes this attribute saves to their data. Now this is fine, but I want to make it so whenever someone rates someone they can never rate them again. I assume to do this I would need to also save every rating I give alongside who I give it to, and then somehow retrieve and then check if I’ve already given this player a rating every time I rate someone else.

All this seems out of my reach and I’d really appreciate it if someone could shed some light on how I might go about doing this, and what the simplest way to go about doing this is.

What you’re going to need to do is make an IntValue when someone rates the player. Parent this IntValue to a folder in the target player’s called “Raters.” Set this IntValue’s value to the rater’s UserId, and set the IntValue’s name to the rater’s name. When a player goes to rate another player, you’ll check this folder for an IntValue with their UserId, and if one is found, it won’t let them rate the player.

local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
local Players = game:GetService("Players")

function folderToTable(folder)
	local _table = {};
	for _, v in next, folder:GetChildren() do
		_table[v.Name] = v.Value; --Turns the int value into an entry in this table
	end
	return _table;
end

function tableToValues(_table, folder)
	for k, v in next, _table do
		local value = Instance.new("IntValue");
		value.Name = k;
		value.Value = v;
		value.Parent = folder;
	end
end

local onJoin = function(player)
	local getData = Datastore:GetAsync(player.UserId)
	if getData[1] then
		player:SetAttribute("AverageRating", getData[1]);
		player:SetAttribute("RatingsReceived", getData[2]);
		player:SetAttribute("RatingsGiven", getData[3]);
		
		local raterData = getData[4] or {};
		local raters = Instance.new("Folder");
		raters.Name = "Raters";
		raters.Parent = player;
		
		tableToValues(raterData, raters);
	end
end

local onLeave = function(player)
	local data = {}
	table.insert(data, player:GetAttribute("AverageRating"))
	table.insert(data, player:GetAttribute("RatingsReceived"))
	table.insert(data, player:GetAttribute("RatingsGiven"))
	table.insert(data, folderToTable(player.Raters));

	Datastore:SetAsync(player.UserId, data)
end


Players.PlayerRemoving:Connect(onJoin)
Players.PlayerAdded:Connect(onJoin)

Ah I see, so every time someone joins they are given the folder of all their raters, and whenever I need I can just iterate through that folder to see if I’ve already rated someone. This seems like it’ll work, thanks! I find myself usually intimidated by anything datastore that doesn’t just involve a few saved values.

1 Like