local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore") myDataStore:SetAsync(script.Parent.Value)
there is an output error that reads, "DataStore can't be accessed from client", how would i fix that. the script is inside of a imagelable that is inside of a frame that is inside of the ,gui and it is a local script. Im new to datastore and is just using it, please help!
Hello. The reason it says that is to prevent exploiters from changing values. Instead, use a RemoteEvent. You would insert a RemoteEvent in ReplicatedStorage and rename it to "ChangeData". Then change your LocalScript to this:
game:GetService("ReplicatedStorage"):WaitForChild("ChangeData"):FireServer()
This will fire the RemoteEvent for the server. Now insert a Server/Regular Script into ServerScriptService. Insert the following code:
local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore") game:GetService("ReplicatedStorage"):WaitForChild("ChangeData").OnServerEvent:Connect(function(player) myDataStore:SetAsync(script.Parent.Value) end)
The OnServerEvent event will fire when the RemoteEvent is fired for the server. Then it changed the value. Also, the "player" parameter is automatically made when a client fires server. By the way, this would not work since you did not tell it what to save and the key to save it. Anyways, please accept and upvote this answer if it helps.
It kinda explains whats the problem is. The LocalScript
can only access the player. So trying to use a LocalScript
will only affect the Client
, not the Server
. The DataStore is accessed by Servers
as if it was saved to the Client
the save file will be removed when the player leaves, not forever. So try using a Script
where it accesses the Server
, while the LocalScript
can access PlayerGui
, PlayerScripts
and StarterPack
stored in LocalPlayer
. Try change it from a LocalScript
to a Script
. Hope this helps!