Please, how can i add a IntValue DataSave from this script ->

  1. What do you want to achieve? Hello, I finished my game but I have a problem to save an IntValue from my script. The value is called donation and you can find it in the first line of the script. Honestly I tried so many different scripts without success, I also asked for help unfortunately it still doesn’t work. I have put the main part of the script. Everything in the script works, only the saving of the IntValue “Donation” doesn’t work (I removed this part of my script to make it more readable) Thanks
game.Players.PlayerAdded:Connect(function(plr)
	local creatures = Instance.new("Folder")
	creatures.Parent = plr
	creatures.Name = "Creatures"

	local donation = Instance.new("IntValue") -- I want to save this value
	donation.Parent = plr --//////
	donation.Name = "Donator"  --//////

	local data = SaveData:GetAsync(plr.UserId)
	
	if data then 
		for _, creature in pairs(data) do
			if workspace.Creatures:FindFirstChild(creature) then
				local bool = Instance.new("BoolValue")
				bool.Name = creature
				bool.Parent = creatures
				for i,v in pairs(game.Workspace.Creatures:GetChildren()) do
					v.CanTouch = true
				end
			end
		end
	else
		for i,v in pairs(game.Workspace.Creatures:GetChildren()) do
			v.CanTouch = true
			print("NO_DATA")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local CreaturesName = {}
	for _, creature in pairs(plr.Creatures:GetChildren()) do
		table.insert(CreaturesName, creature.Name)
	end
	SaveData:SetAsync(plr.UserId, CreaturesName)
end)

Can someone tell me if I have to go through a second DataStoreService to save this value? Because nothing seems to work because of the creatures file, I feel like I tried everything

Try changing your script to this:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local data = SaveData:GetAsync(plr.UserId) or {}

	local creatures = Instance.new("Folder")
	creatures.Name = "Creatures"
	creatures.Parent = plr

	local donation = Instance.new("IntValue")
	donation.Name = "Donator"
	donation.Value = data.Donation or 0
	donation.Parent = plr

	if data.CreaturesName then 
		for _, creature in pairs(data.CreaturesName) do
			if workspace.Creatures:FindFirstChild(creature) then
				local bool = Instance.new("BoolValue")
				bool.Name = creature
				bool.Parent = creatures
				
				for i,v in pairs(workspace.Creatures:GetChildren()) do
					v.CanTouch = true
				end
			end
		end
	else
		for i, child in ipairs(workspace.Creatures:GetChildren()) do
			child.CanTouch = true
			
			print("NO_DATA")
		end
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local Data = {
		CreaturesName = {},
		Donation = plr.Donator.Value
	}
	
	for _, creature in pairs(plr.Creatures:GetChildren()) do
		table.insert(data.CreaturesName, creature.Name)
	end
	
	SaveData:SetAsync(plr.UserId, Data)
end)

Also, goodluck on your game.

1 Like

Thank you for your help, it works very well ! And thank you

1 Like