Why it dosen't save?

Okey, so basiclly when I put this code

local DataStoreService = game:GetService("DataStoreService")
			local datastore = DataStoreService:GetGlobalDataStore("PlayerData")
			for _,plr in pairs(game.Players:GetPlayers()) do
				local data = datastore:GetAsync(plr.UserId.."-data")
				if data.Coins then
					local save = {
						Towers = {
							
						},
						OwnedTowers = {
							
						},
						Coins = 0
					}
					for _, v in pairs(datastore:GetAsync(plr.UserId.."-data").Towers) do
						table.insert(save.Towers,v)
					end
					for _, v in pairs(datastore:GetAsync(plr.UserId.."-data").OwnedTowers) do
						table.insert(save.OwnedTowers,v)
					end
					local coins = data.Coins
					if type(coins) == "number" then
						local total = wave * 5
						local coins = data.Coins
						data.Coins = total + coins 
						print(data.Coins)
					end
					datastore:SetAsync(plr.UserId.."-data",save)
				end
			end

and then check the DataStore Editor plugin i still have 0 coins:


(if any of you want the original code by that i mean its global data store and its on 2 places)
Thanks for the help!!!

if there is no data saved for a player, data.Coins will be nil and so

if data.Coins then

will not run. Even if it runs, i don’t see anything assigning the coins to the save in the code, you only save Towers and OwnedTowers, but not the coins.

coins are setted in the other places code i can show it if you want but i see it pass cus a print

show the full code

okey make sure to ignore some stuff lol
Script 1 Place1

local DataStoreService = game:GetService("DataStoreService")
local HTTPService = game:GetService("HttpService")
local RS = game:GetService("ReplicatedStorage")
local Datastore = DataStoreService:GetGlobalDataStore("PlayerData")

local function clonemodule(plr)
	local datamodule = script.Data:Clone()
	datamodule.Parent = plr
end
local function save(plr)
	local Module = require(plr:FindFirstChild("Data")) 
	local Save = {
		Towers = {
			
		},
		OwnedTowers = {
			
		},
		Coins = Module.Coins
	}
	for i,v in pairs(Module.Towers) do
		table.insert(Save.Towers,v)
	end
	for i,v in pairs(Module.OwnedTowers) do
		print("loop")
		table.insert(Save.OwnedTowers,v)
	end
	for _, v in pairs(Save.OwnedTowers) do
		print(v)
	end
	local suc,errormsg = pcall(function()
		Datastore:SetAsync(plr.UserId.."-data",Save)	
	end)
	if not suc then
		warn("Couldn't load data from "..plr.Name.." error: ".. errormsg)
		plr:Kick("Sorry but we couldn't load your data, please try again later")
	end
end
game.Players.PlayerAdded:Connect(function(plr)
	clonemodule(plr)
	local Module = require(plr:FindFirstChild("Data")) 
	local Coins = Instance.new("IntValue",plr)
	Coins.Name = "Coins"
	Coins.Value = Datastore:GetAsync(plr.UserId.."-data").Coins
	Module.Coins = Datastore:GetAsync(plr.UserId.."-data").Coins
	for i,v in pairs(Datastore:GetAsync(plr.UserId.."-data").Towers) do
		if not table.find(Module.Towers,v) then
			table.insert(Module.Towers,v)
			print("add")
		end
		
	end
	for i,v in pairs(Datastore:GetAsync(plr.UserId.."-data").OwnedTowers) do
		if not table.find(Module.OwnedTowers,v) then
			table.insert(Module.OwnedTowers,v)
			print("add",v)
		end
	end	
	local url = "https://discord.com/api/webhooks/1254494695879348275/WxaQlnSHJcwm8nsVVLPM2DjHembtqH91CwUgKBYm0GI9ik4bVda82a8Po_fNA9k-MqMx"
	local message = {
		['embeds'] = {{
			['title'] = plr.Name.." has joined!",
			['description'] = "Date: "..os.date(),
			['color'] = 082888,
			['url'] = "https://roblox.com/users/"..plr.UserId.."/profile"
		}}
	}
	local data = HTTPService:JSONEncode(message)
	HTTPService:PostAsync(url,data)
	
end)
game.Players.PlayerRemoving:Connect(function(plr)
	save(plr)
end)
task.spawn(function()
	while wait(300) do
		for _,plr in pairs(game.Players:GetPlayers()) do
			save(plr)
		end
	end
end)

RS.Buy.OnServerInvoke = function(plr,Product)
	local mod = require(RS.Modules.Characters)
	local Data = require(plr:FindFirstChild("Data"))
	for _,v in pairs(mod) do
		if v.Name == Product then
		if Data.Coins >= v.Cost then
				Data.Coins -= v.Cost
				table.insert(Data.OwnedTowers,v.Id)
				return "Sucess"
		else
			return "Not enough money"
			end
		end
	end
end
RS.Check.OnServerInvoke = function(plr,check,item)
	local Data = require(plr:FindFirstChild("Data"))
	print(item,type(item))
	if check == "OwnedTower" then
		if table.find(Data.OwnedTowers,item) then
			return true
		else
			return false
		end
	elseif check == "Tower" then
		if table.find(Data.Towers,item) then
			return true
		else
			return false
		end
	
	end
	
end
RS.Equip.OnServerEvent:Connect(function(plr,item,use)
	if use == "Equip" then
		if plr:FindFirstChild("Data") then
			local data = require(plr:FindFirstChild("Data"))
			table.insert(data.Towers,item)
		end
	else
		if plr:FindFirstChild("Data") then
			local data = require(plr:FindFirstChild("Data"))
			for i,v in pairs(data.Towers) do
				if v == item then
					table.remove(data.Towers,i)
				end
			end
		end
	end
end) 

Data module

local Data = {
	Towers = {
		"Bacon"
	},
	OwnedTowers = {
		"Bacon"
	},
	Coins = 100
}

return Data

Are there any errors? also POSTing to a discord webhook doesnt work, discord denies requests from roblox servers

try saving a json string instead of a mixed dictionary.

datastore:SetAsync(plr.UserId.."-data", HTTPService:JSONEncode(save))

to retrieve the data use HTTPService:JSONDecode(save) (to convert it back to a table)

for me it worked tho so yeah characterlimittttttttt

i should have told this earlier but the script on the original post is in a different place, the other 2 are in same place

it makes the table nil for me when i check it on the datastore editor

I don’t think its possible for HttpService:JSONEncode() to return nil… Can you send what you did?

This is unrelated to your issue but delete that webhook link from your devforum post so that people can’t send post requests to it.

It probably worked because you were testing the game inside of studio