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

DataStore won't work, help?

I'm trying to make a GUI that only appears the first time you ever play a game. Now, you may recognize this question, as I asked it relating to Data Persistence. However, someone recommended that I use DataStore instead. So that's what I'm doing. I read up on the wiki, and I built a script based on what I read, but I'm still new to it. The script I made won't fire, and there are no errors in console (In test or in game). Here's my script:

local Read = game:GetService("DataStoreService"):GetDataStore("Read")

local check = Read:GetAsync("Read", function(Value)
	if Value == nil then
	game.ReplicatedStorage.Message:Clone().Parent = game.Players.LocalPlayer.PlayerGui
	Read:SetAsync("Read", "yes")
end
end

The script is a normal script located inside workspace. All help is appreciated!

1 answer

You can't use game.Players.LocalPlayer in a Script (non- LocalScript). Instead, write a script that triggers each time a Player joins:

game:GetService("Players").PlayerAdded:connect(function(plr)
	-- Connect each time a player joins
	-- Reference Data Store unique to each player
	local Read = game:GetService("DataStoreService"):GetDataStore("Read"..plr.UserId) 

	-- Find if the Player's datastore has ever been changed to "yes"
	-- If it hasn't, clone a Message into their PlayerGui, then set DataStore to "yes"
	local check = Read:GetAsync("Read"..UserId, function(Value)
		if Value == nil then
			game.ReplicatedStorage.Message:Clone().Parent = plr.PlayerGui
			Read:SetAsync("Read"..UserId, "yes")
		end
	end)
end)

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