Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Please Help! Remote events and Datastores?

Hello! I am very new to Roblox Lua, and I am thoroughly confused about using remote events to send client values to the server so that the datastore can save the data changed by the client. For example, my players collect coins which updates their amount of coins client sided. I need help writing a script that allows the client values to be sent to the server so that the datastore can save the amount of coins. I need help understanding the writing of the remote event so that it saves the data to the datastore. Please also refer to where to place the scripts of the remote event so that I know to make sure that it is correct. Here is my datastore script so you reference if you choose to help me. I've tried everything on the internet, but now I am trying script helpers. Here is my datastore script:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local variables = Instance.new("Folder")
	variables.Name = "variables"
	variables.Parent = player
	
	local Studs = Instance.new("IntValue")
	Studs.Name = "Studs"
	Studs.Parent = leaderstats
	
	local StudsValue = Instance.new("IntValue")
	StudsValue.Name = "StudsValue"
	StudsValue.Parent = variables
	
	local IsRunning = Instance.new("BoolValue")
	IsRunning.Name = "IsRunning"
	IsRunning.Parent = variables
	
	local playerUserId = "Player_"..player.UserId

	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
	
	if success then
		Studs.Value = data.Studs
		StudsValue.Value = data.StudsValue
		IsRunning.Value = data.IsRunning
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data = {
		Studs = player.leaderstats.Studs.Value;
		StudsValue = player.variables.StudsValue.Value;
		IsRunning = player.variables.IsRunning.Value;	
	}
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error when saving data!")
		warn(errormessage)
	end
	
end)

This is how I write my datastore scripts which works for me, if there is a better way, please tell me. This is an example.

1 answer

✓ Accepted answer

Hey guys, me again. I am an idiot. I was trying to use the RemoteEvent to quote on quote "save" data instead of firing an event where the same changes are made to the server's values. Thanks anyways, however. A lot of guides on here helped me out tremendously.

1 comment

0

youre not an idiot glad you got it working.

This discussion is preserved as it was. New answers, comments, and votes are closed.