Script Doesnt Work

My goal it makes a script which can save rolls value and aura value. But idk why its didn’t work, and this script don’t even print a something when player leaving.

local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("Rolls")
local event = game:GetService("ReplicatedStorage").CurrentAuraSystem

local function load(player)
	local character = player.Character
	local Rolls = player:WaitForChild("leaderstats"):WaitForChild("Rolls")
	local Aura = player:WaitForChild("leaderstats").hidden:WaitForChild("Aura")

	local success, metadatas = pcall(function()
		return datastore:GetAsync(player.UserId) 
	end)

	if metadatas then
		Rolls.Value = metadatas.Rolls
		if not metadatas.Aura then
			Aura.Value = "None"
		else
			Aura.Value = metadatas.Aura
		end
		
	else
		Rolls.Value = 0
		Aura.Value = "None"
	end
end

players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	local hidden = Instance.new("Folder", leaderstats)
	hidden.Name = "hidden"
	local Rolls = Instance.new("IntValue", leaderstats)
	Rolls.Name = "Rolls"
	local Aura = Instance.new("StringValue", hidden)
	Aura.Name = "Aura"

	load(plr)
end)

players.PlayerRemoving:Connect(function(plr)
	local metadatas = {["Rolls"] = plr:WaitForChild("leaderstats").Rolls.Value ,["Aura"] = plr:WaitForChild("leaderstats").hidden.Aura.Value}
	local success, errormsg = pcall(function()
		datastore:SetAsync(plr.UserId, metadatas)
	end)
	
	if success then
		print("cool bro your data saved so ezz")
	else
		print(errormsg)
	end
end)
1 Like

Your script appears mostly correct, but there might be an issue with how you’re handling nil values for Aura when loading data from the DataStore. Here’s a revised version of your script that should work properly hope that helps!:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("Rolls")

local function load(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if not leaderstats then
		leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player
	end
	
	local Rolls = leaderstats:FindFirstChild("Rolls")
	if not Rolls then
		Rolls = Instance.new("IntValue")
		Rolls.Name = "Rolls"
		Rolls.Parent = leaderstats
	end
	
	local hidden = leaderstats:FindFirstChild("hidden")
	if not hidden then
		hidden = Instance.new("Folder")
		hidden.Name = "hidden"
		hidden.Parent = leaderstats
	end
	
	local Aura = hidden:FindFirstChild("Aura")
	if not Aura then
		Aura = Instance.new("StringValue")
		Aura.Name = "Aura"
		Aura.Parent = hidden
	end

	local success, metadatas = pcall(function()
		return datastore:GetAsync(player.UserId) 
	end)

	if success and metadatas then
		Rolls.Value = metadatas.Rolls or 0
		Aura.Value = metadatas.Aura or "None"
	else
		Rolls.Value = 0
		Aura.Value = "None"
	end
end

Players.PlayerAdded:Connect(function(player)
	load(player)
end)

Players.PlayerRemoving:Connect(function(player)
	local Rolls = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Rolls")
	local Aura = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("hidden") and player.leaderstats.hidden:FindFirstChild("Aura")
	if Rolls and Aura then
		local metadatas = {
			Rolls = Rolls.Value,
			Aura = Aura.Value
		}
		local success, errormsg = pcall(function()
			datastore:SetAsync(player.UserId, metadatas)
		end)
		
		if success then
			print("Data saved successfully for player: " .. player.Name)
		else
			warn("Failed to save data for player: " .. player.Name .. ", Error: " .. errormsg)
		end
	end
end)

Changes made:

  • Improved handling of leaderstats, Rolls, and Aura creation and retrieval to ensure they’re properly created if missing.

  • Simplified the code for setting Rolls and Aura values when loading from the DataStore.

  • Added checks to ensure Rolls and Aura values are retrieved properly when saving data.

  • Changed print to warn when there’s an error saving data to better capture it in the Roblox Studio output.

it’s still doesn’t work and still don’t even showing a error :sob:
what i mean: this code doesn’t showing a part print when player leaving

First, this line is not used anywhere. It isn’t causing any visible issues, however, it is redundant.

Second, the issue with printing seems to occur with saving the data.

Change this:

To this:

local success, errormsg = pcall(function()
	return datastore:SetAsync(plr.UserId, metadatas)
end)

Let me know if this works. It seems to work for me.

EDIT: This only seems to work when I do a local server test. Doing this keeps the server open even after the player leaves.

This is doesn’t work. BUT!
Aura Value its string value, and when i am trying to load only rolls (leaving from game having value Aura == nil.) its working

My guess is that you are having issues loading Aura, as @Mevvey mentioned above. I don’t have a fix for that right now, but will get back to you if I think of something.

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