Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

It says "DataStore can't be accessed from client" how would I fix that?

Asked by 3 years ago
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!

2 answers

Log in to vote
1
Answered by 3 years ago

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.

0
How would you save it a key? Spiyder1 81 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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!

Answer this question