Hello, i am trying to make a script where it finds the players datastore values and sees if its true to execute code, transfers between the server and client. (dont know if this is optimal)
I am not good with coding datastores so im not sure if it worked but ive been modifying it a little bit everytime it doesnt work so yeah…
as i stated before i just modified it a bit every time it didnt work for just trying to solve it so i just took this problem to the dev forum.
server script
game.Players.PlayerAdded:Connect(function(plr)
local ds = game:GetService("DataStoreService"):GetDataStore("exampleyes") --replaced so its not the real one
local plrdata = ds:GetAsync(plr.UserId .. "exampleexamplexamply") --replaced it so its not the real one
if plrdata.Muzy == true then
game.ReplicatedStorage.RemoteEvents.muztrue:FireClient(plr)
end
end)
Firstly, define your DataStore outside of the connection.
local ds = game:GetService("DataStoreService"):GetDataStore("exampleyes")
Then, when the player is added to the game, you will want to ensure the value exists. However, you will want to make sure the player data exists first. You can do this by doing the following (in the connection):
You’ll also want to use a pcall since you’re working with DataStores.
local plrdata
local Success, Error = pcall(function() -- wrapped in a pcall function just in case it errors because of a rare roblox error or something
plrdata = ds:GetAsync(plr.UserId .. "exampleexamplexamply")
end)
if Success then -- ensures it successfully gets the data without ROBLOX issues, etc.
if plrdata then -- checks to make sure it exists
if plrdata.Muzy == true then -- checks to see if value is true
-- fire client event here
end
end
else
print(Error)
end
This is how I use my DataStores, so YMMV when it comes to advice from other scripters.
game.Players.PlayerAdded:Connect(function(plr)
local ds = game:GetService("DataStoreService"):GetDataStore("exampleyes")
local plrdataPromise = ds:GetAsync(plr.UserId .. "exampleexamplexamply")
plrdataPromise:andThen(function(plrdata)
if plrdata.Muzy == true then
game.ReplicatedStorage.RemoteEvents.muztrue:FireClient(plr)
end
end)
end)