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

Why I can't save the Bool Value?

Basically, when player clicks the part, the boolvalue of boughtAK is supposed to be true. And when player leaves, the value is supposed to be saved, but it's not saving, any help?

local DS = game:GetService("DataStoreService"):GetDataStore("Exp")


game.Players.PlayerAdded:Connect(function(player)
	
	local boughtAK = Instance.new("BoolValue", player)
	boughtAK.Name = "boughtAK"
	
	workspace.Part.ClickDetector.MouseClick:Connect(function(clickPlayer)
		clickPlayer.boughtAK.Value = true
	end)
	
	
	local stats = DS:GetAsync(player.UserId)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local allSaves = {}
	
	table.insert(allSaves, player.boughtAK.Value)
	DS:SetAsync(player.UserId, allSaves)
	
end)

3 comments

1

It will not work because you are not loading in the data.

0

Thanks @Soban06

0

You're most welcome.

3 answers

✓ Accepted answer

Everything should be fine, you just never loaded the data


local DS = game:GetService("DataStoreService"):GetDataStore("Exp")


game.Players.PlayerAdded:Connect(function(player)

    local boughtAK = Instance.new("BoolValue", player)
    boughtAK.Name = "boughtAK"

    workspace.Part.ClickDetector.MouseClick:Connect(function(clickPlayer)
        clickPlayer.boughtAK.Value = true
    end)


    local stats = DS:GetAsync(player.UserId)
    boughtAK.Value = stats[1] -- I think this is how you load it, not very good with DataStores
end)

game.Players.PlayerRemoving:Connect(function(player)
    local allSaves = {}

    table.insert(allSaves, player.boughtAK.Value)
    DS:SetAsync(player.UserId, allSaves)

end)

1 comment

0

Change the value to boughtAK.Value = stats[1] or false in case someone has no data

You havent configured your table.insert correctly. First yoiu have the table, which you did, but thrn you need to add the position of which your inserted value will take once in the table. For this, add

#AllSaves +1

to put it in the first pos. Then, for the third parameter, add the thing you want to insert. Hope this helps. P.s. prob best if you don't parent your instance in the 2nd parameter as that is slower, but it's fine anyway

Try to change this inside the player added event. Instead of saying local stats = DS:GetAsync(player.UserId), do this:

local data
local success, errormessage = pcall(function()
	data = DS:GetAsync(player.UserId)
end)

if success and data then
	boughtAK.Value = stats.boughtAK
else
	print("Error while fetching the data!")
	warn(errormessage)
end

And inside the player removing:


local success, errormessage = pcall(function()
	local data = {
		boughtAK = player.boughtAK.Value;
	}

	DS:SetAsync(player.UserId, data)
end)

if success then
	print("Successfully saved data!")
else
	print("Error while saving the data!")
	warn(errormessage)
end

Let me know if it helped.

2 comments

0

I didnt load the data, the guy that commented on my post was right, that was the mistake

0

Yeah that was me.

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