Why doesn't this datastore script work?

Yo so i got this script:

local DataStoreService = game:GetService("DataStoreService")

local ds = DataStoreService:GetDataStore("datatest")

game.Players.PlayerAdded:Connect(function(plr)
	local Folder = Instance.new("Folder",plr)
	Folder.Name = "test"
	local BoolValue = Instance.new("BoolValue",Folder)
	BoolValue.Name = "BoolValue"

	local dataofuser = ds:GetAsync(plr.UserId)
	if dataofuser ~= nil then -- anything but nil
		print("Found data for " .. plr.Name)
		BoolValue.Value = BoolValue.Value
	else
		print("Replacing no data with new data.")
		BoolValue = false
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local datasave = {}
	table.insert(datasave, plr:WaitForChild("test"):WaitForChild("BoolValue").Value)
	local success,response = pcall(function()
		ds:SetAsync(plr.UserId, datasave)
	end)

	if success then
		print("succesfully saved data of " .. plr.Name)
	else
		warn(response)
	end
end)

but for some reason it doesn’t work. why?

2 Likes

Have you made sure that access to api services is turned on in your experience?

Please be more descriptive. What happens? What doesn’t happen? What does the console look like?

it just doesn’t save the int value. i got a script that sets it to true but when i rejoin its set to false.

Cleaned it up and fixed a few errors. (I hope)

local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("datatest")

game.Players.PlayerAdded:Connect(function(plr)
	local Folder = Instance.new("Folder", plr)
	Folder.Name = "test"
	local BoolValue = Instance.new("BoolValue", Folder)
	BoolValue.Name = "BoolValue"

	local dataofuser = ds:GetAsync(plr.UserId)
	if dataofuser then
		BoolValue.Value = dataofuser[1] 
	else
		print("Replacing no data with new data.")
		BoolValue.Value = false
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local datasave = {plr:WaitForChild("test"):WaitForChild("BoolValue").Value}

	local success, response = pcall(function()
		ds:SetAsync(plr.UserId, datasave)
	end)

	if success then
		print("Successfully saved data of " .. plr.Name)
	else
		warn(response)
	end
end)

Not tested …

1 Like

It actually works (i think) but i also got a little problem but i will talk about it later cuz i gtg for now.

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