Broken datastore script

I have a script that doesn’t seem to be working.

local moneyStore = DS:GetDataStore("Money")
local remote = game:GetService("ReplicatedStorage").CashManager

game.Players.PlayerAdded:Connect(function(player)
	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Cash"
	money.Value = 100
	money.Parent = leaderstats
	
	if success then
		money.Value = moneyValue
	else
		print("Failed to Load Data")
	end

	while wait(3600) do
		money.Value += 10000
	end
	
	local function save(player) 
	local success, err = pcall(function()
			moneyStore:SetAsync("player_"..player.UserId, player.leaderstats.Money.Value)
		end)
	end
		if success then
			print("Saved Data")
			
		else
			print("Failed to Save Data")
		end
	end
	
	local function autosave()
		while wait(10) do
			for i, player in pairs (game:GetService("Players"):GetPlayers()) do
				save(player) 
			end
		end
	end
	
	remote.OnServerEvent:Connect(function(player, amount)
		player.leaderstats.money.Value += amount
	end)

spawn(autosave)```
1 Like

Try this code:
local moneyStore = DS:GetDataStore("Money") local remote = game:GetService("ReplicatedStorage").CashManager

game.Players.PlayerAdded:Connect(function(player)
	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Cash"
	money.Value = 100
	money.Parent = leaderstats
	
	if success then
		money.Value = moneyValue
	else
		print("Failed to Load Data")
	end

	spawn(function()
        while wait(3600) do
		    money.Value += 10000
        end
	end)
	
	local function save(player) 
	local success, err = pcall(function()
			moneyStore:SetAsync("player_"..player.UserId, player.leaderstats.Money.Value)
		end)
	end
		if success then
			print("Saved Data")
			
		else
			print("Failed to Save Data")
		end
	end
	
	local function autosave()
		while wait(10) do
			for i, player in pairs (game:GetService("Players"):GetPlayers()) do
				save(player) 
			end
		end
	end
	
	remote.OnServerEvent:Connect(function(player, amount)
		player.leaderstats.money.Value += amount
	end)

spawn(autosave)

same error

idk why, but its kinda important

Can you show the error?_______

theres no error in output, just in the script “local” and “save” are underlined

Hover your mouse on the underlined text and you will see the error.

try this, you forgot a ) after an end since you made a :Connect

local moneyStore = DS:GetDataStore("Money")
local remote = game:GetService("ReplicatedStorage").CashManager

game.Players.PlayerAdded:Connect(function(player)
	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Cash"
	money.Value = 100
	money.Parent = leaderstats

	if success then
		money.Value = moneyValue
	else
		print("Failed to Load Data")
	end

	while wait(3600) do
		money.Value += 10000
	end

	local function save(player) 
		local success, err = pcall(function()
			moneyStore:SetAsync("player_"..player.UserId, player.leaderstats.Money.Value)
		end)
	end
	if success then
		print("Saved Data")

	else
		print("Failed to Save Data")
	end
end)

local function autosave()
	while wait(10) do
		for i, player in pairs (game:GetService("Players"):GetPlayers()) do
			save(player) 
		end
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.money.Value += amount
end)

spawn(autosave)

that fixed the local error but save sill has an underline

which line has the save with the underline?

image

image

Do you have something outside of the script you are referencing? Because CashManager might have a problem in it. That could be why the save is underlined.

Cashmanager is a remote event for changing the value of the moeny

1 Like

oh so do you have any other script to actually run the remote event? Or only this one?

I have another one, but its not active rn

sorry about my last reply, but try to check if you misspelled anything. Sorry I wasn’t able to help

If you just want to get rid of the line underneath save then remove local from the function:

local function save(player)

to

function save(player)

I think I fixed it! When I fixed the save() thing, a new thing got underlined which was the last line and the underlined thing was autosave, it was because you put a local before the function when its inside of another function so the value was not transferring.

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Money")
local remote = game:GetService("ReplicatedStorage").CashManager

game.Players.PlayerAdded:Connect(function(player)
	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Cash"
	money.Value = 100
	money.Parent = leaderstats

	if success then
		money.Value = moneyValue
	else
		print("Failed to Load Data")
	end

	while wait(3600) do
		money.Value += 10000
	end

	local function save(player) 
		local success, err = pcall(function()
			moneyStore:SetAsync("player_"..player.UserId, player.leaderstats.Money.Value)
			
	end)
		
	if success then
		print("Saved Data")
	else
		print("Failed to Save Data")
	end
end

	function autosave()
		while wait(10) do
			for i, player in pairs (game:GetService("Players"):GetPlayers()) do
				save(player) 
			end
		end
	end
end)

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.money.Value += amount
end)

spawn(autosave)