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

My datastore isn't saving right and I don't know what's wrong?

When I save my data in game and use GetAsync it gets the data back just fine, but if i rejoin and use GetAsync the data is back to 0.

for _,v in pairs(game.Players:GetChildren())do	
			--saving god power
			local success, err = pcall(function()
					--print(v.PlayerInfo.Stats.Followers.Value)
			return godPower:SetAsync(v.UserId, v.leaderstats:FindFirstChild("God Power").Value)
			end)

			if success then
				print("godpower saved")
			end	
			
			
			local success, err = pcall(function()--god power
				currentGP = godPower:GetAsync(v.UserId) or 0
			end)
			print("SAVED: "..currentGP)
end

this prints "SAVED: 146"

Players.PlayerAdded:Connect(function(player)

	local success, err = pcall(function()--god power
		currentGP = godPower:GetAsync(player.UserId) or 0
	end)
	
	if success then
		print("power:"..currentGP)
		player.leaderstats:FindFirstChild("God Power").Value = currentGP
	end
end)

this prints "power:0"

1 comment

0

Try to remove the get async in the first script. Maybe that helps.

1 answer

Ok so I found the problem

local function onCharacterRemoving(character)
	
	-- Get the player and their HumanoidRootPart and save their death location
	local player = Players:GetPlayerFromCharacter(character)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	local player = Players:GetPlayerFromCharacter(character)
	goddied(player)
end

in the goddied function it sets the godpower to 0 again, but this happens when the player joins because

local function onPlayerAdded(player)
	
	player.CharacterAdded:Connect(onCharacterAdded)
	player.CharacterRemoving:Connect(onCharacterRemoving)
	--player info
	local playerInfo = game.ServerStorage.PlayerInfo:Clone()
	playerInfo.Parent = player

	
end

onCharacterRemoving is fired once when the player joins so to avoid this problem, we just need to not have onCharacterRemoving run the first time the player joins. Hope this helps if anyone else ever runs into the same issue.

2 comments

0

ok my bad it's not that its fired once when the player joins but rather when the player leaves it also removes the char not just when they die

0

ok my bad it's not that its fired once when the player joins but rather when the player leaves it also removes the char not just when they die

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