I'm attempting to create a sort of "data center" for a clan.

I've created a datastore that spawns certain values whenever a player joins into ReplicatedStorage, values that have unique IDs according to the player.

Anyway, that all works fine, but the problem is that if a new server starts, those values don't automatically spawn with their saved values unless that player joins the server again.

How would I make the datastore "remember" to insert those values back into replicatedstorage again even if the player those values were assigned to hadn't joined the server yet?

Here's my code for reference

local datastore = game:GetService("DataStoreService"):GetDataStore("rP")
print("Got datastore!")
local serverstorage = game:GetService("ReplicatedStorage")
print("Got serverstorage!")

game.Players.PlayerAdded:connect(function(plyr)
	local rp = Instance.new("IntValue")
	rp.Parent = serverstorage
	rp.Name = "rP-"..plyr.Name
	print("rp made!")
	local key = "plyr"..plyr.userId
	print("Key generated!")
	local username = Instance.new("StringValue")
	username.Parent = serverstorage
	username.Name = plyr.Name
	username.Value = plyr.Name
	print("Username has been created and inserted into serverstorage.")
	
	local userrank = Instance.new("IntValue")
	userrank.Parent = serverstorage
	userrank.Name = plyr.Name.."-rank"
	userrank.Value = plyr:GetRankInGroup(2530182)
	
	local userid = Instance.new("IntValue")
	userid.Parent = serverstorage
	userid.Name = plyr.Name.."-id"
	userid.Value = plyr.userId
	print("userid acquired!")
	
	local savedvalue = datastore:GetAsync(key)
	print("Savedvalue loaded")
	
	if savedvalue and contents then
		savedvalue[1] = rp.Value
		savedvalue[2] = username.Value
		savedvalue[3] = userid.Value
		savedvalue[4] = userrank.Value
		print("Savedvalue table generated")
	else
		datastore:SetAsync(key, {rp.Value, username.Value,userid.Value,userrank.Value})
		print("Items saved")
	end
end)

Sorry if my question sounded a bit confusing. If you need me to elaborate, please let me know.

1 comment

1

use datastore:SetAsync(key,savedvalue)