"If players datastore.value == true then"

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)

local script

game.ReplicatedStorage.RemoteEvents.muztrue.OnClientEvent:Connect(function()
	game.Workspace.NPCS.muzygatochal.Position = Vector3.new(149.847, 6.348, 8.331) --mr muzy
	game.Workspace.Teleporter["muz challenge room"].Teleporter.Position = Vector3.new(142.472,5.044, 7.792)
	print("u got")
end)

please helppppp

are you trying to get a players name from ur datastore?

im trying to get the value “Muzy” from the datastore and see if the player has it

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)

Thank you! it worked perfectly. (im not used to coding with datastores so this is awesome!!!)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.