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

How to use RemoteEvents/DataStore?

I just found out that I can't use DataStore's in client side scripts, so the only way I can think of this is by using RemoteEvents, for example if a player buys something from a shop it will call a RemoteEvent which then prints the players name to that Event as well as the Price to the player. But how do I do this?

LocalScript:

-- Something like this? Not sure..

Event:FireServer(Player.Name, Price)

Receiving Script: ??

1 answer

✓ Accepted answer

Okay, so. First of all you will need a RemoteEvent. The best place to leave this would be ReplicatedStorage, as that gets replicated to the server and client (so you won't have the problems related with that). Name it something obvious (for this example we will use the name "DataStoreEvent")

ServerSide:

local Event = Instance.new("RemoteEvent",game:GetService("ReplicatedStorage"))
Event.Name = "DataStoreEvent"

SaveToStore = function(Key, Data)
	--You can do this stuff
end

LoadFromStore = function(Key)
	--You can also do this stuff
	return Data
end

Event.OnServerEvent:connect(function(Player, Type, Key, Data) --The sending player is always the first argument
	if (Type == "SAVE") then
		SaveToStore(Key, Data)
	elseif (Type == "LOAD") then
		local Data = LoadFromStore(Key)
		Event:FireClient(Player, Key, Data) --Send the data back to the client
	end
end)

ClientSide:

local Event = game:GetService("ReplicatedStorage"):WaitForChild("DataStoreEvent")

local LoadedData = nil --Will hold the stuff that the server sends back

SaveToStore = function(Key, Data)
	Event:FireServer("SAVE", Key, Data)
end

LoadFromStore = function(Key)
	Event:FireServer("LOAD", Key)
	repeat wait() until LoadedData --Will pause the thread until the data has been returned
end

Event.OnClientEvent:connect(function(Key, Data)
	LoadedData = Data --Assign the loaded data to the variable
end)

Please comment below if you have more questions, and if you want to look more into RemoteEvents, use this wiki page.

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