[SOLVED] Datastore not saving yet no errors

Hello devs,

I’m making a anti cheat for my game and I’m working on a warning system, I made a datastore save script however whenever the server adds me 1 warning and I end the session, if I rejoin I have 0 warnings.

Script

local warnData = game:GetService("DataStoreService"):GetDataStore("warns1741414__41JFQFIQJADA7AD7A918E")

game.Players.PlayerAdded:Connect(function(plr)
	local AACFolder = Instance.new("Folder")
	AACFolder.Name = "AAC"
	AACFolder.Parent = plr
	
	local warns = Instance.new("NumberValue")
	warns.Name = "Warnings"
	warns.Parent = AACFolder
	
	local data
	local success, errormessage = pcall(function()
		data = warnData:GetAsync(plr.UserId)
	end)
	
	if success then
		warns.Value = data
	else
		warn("[AAC:createfolders] Unexpected Error: Couldn't load warn data")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local data = plr.AAC.Warnings.Value
	
	local success, errormessage = pcall(function()
		warnData:SetAsync(plr.UserId, data)
	end)
	if success then
		print("[AAC] Registered warn data for "..plr.Name.." using USER_ID ("..plr.UserId..")")
	else
		warn("[AAC:createfolders_critical] Could not save data for "..plr.UserId)
		error("[AAC:createfolders_critical] "..errormessage)
	end
end)
1 Like

How are you changing the warnings number to test this?

EDIT:
I’ve tested this myself and it worked.
My only theory is your changing the Warnings value through the client instead of the server.

try this

if success then
	if data then
		warns.Value = data
	else
		warns.Value = 0 -- the player is new
	end
else
	warn("[AAC:createfolders] Unexpected Error: Couldn't load warn data")
end

Nope, I still have 0 warnings after adding myself like 2 warns

How are you changing the Warnings value, can you please tell us.

where is the line where you save the data thingy

The warning’s value is changed in a serversided script, I just have to say a message and it’s adds the stat BEFORE kicking me, I can send the code if needed

When they join the game or leave?

I would like to see the script.

local dds = game:GetService("DataStoreService")
local banData = dds:GetDataStore(game.CreatorId.."CHANGETHIS".."_aac")

game.Players.PlayerAdded:Connect(function(plr)
	local warns = plr:WaitForChild("AAC"):WaitForChild("Warnings").Value
	if banData:GetAsync(plr.UserId,true) then
		plr:Kick("\nServer Connection Failed\nYou have been caught exploiting and have been permanently banned. If this is a error, please contact a admin.")
	else
		plr.Chatted:Connect(function(msg)
			local arg = msg:split(" ")
			if msg == ";" then
				if warns == 3 then
					banData:SetAsync(plr.UserId,true)
					plr:Kick("Session forcefully ended")
				else
					warns += 1
					plr:Kick("Disconnected from server: the usage of ';' is banned")
				end
			end
		end)		
	end
end)

Don’t mind the fact that I don’t use string.find, but this is the script that adds the warning to me

You have to reassign the .Value property of the Warnings NumberValue.

Where you’re doing warns += 1
You have to do:
plr.AAC.Warnings.Value = plr.AAC.Warnings.Value + 1

1 Like

Oh right, I use visual studio code way too much haha, I’ll try it and let you know.

It’s works, thanks for your help.