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

My cash is not saving after I leave the game. This is all in a local script. How do i save it?

--Saving Script

local DSS = game:GetService("DataStoreService")

local datastore = DSS:GetDataStore("GeneralSaveData", "Players")

function generateDataKey(player)
    local ret = "uid_" .. player.userId
    return ret
end

function generateDataTable(player)
    local dataTable = {
     Cash = player.leaderstats.Cash.Value
    }
    return dataTable
end

function saveDataForPlayer(player)
    local key = generateDataKey(player)
    local data = generateDataTable(player)
    datastore:SetAsync(key, data)
end

function inputDataToPlayer(player, data)
    player.leaderstats.Cash.Value = data.Cash
end

function loadDataForPlayer(player)
    local key = generateDataKey(player)
    local data = datastore:GetAsync(key)
    inputDataToPlayer(player, data)
end

game.Players.PlayerAdded:connect(function(player)
    loadDataForPlayer(player) --Load first thing when they join
    player.leaderstats.Cash.Changed:connect(function()
        saveDataForPlayer(player) --Save the data when Cash changes value
print("SaveCash")
    end)
end)

game.Players.PlayerRemoving:connect(saveDataForPlayer) --Save data when the player leaves the game


This is the New Shop script

local player = game.Players.LocalPlayer
local price = script.Parent.Parent.Price
local tools = game.ReplicatedStorage:WaitForChild("Tools")
local tool = tools:FindFirstChild(script.Parent.Parent.ItemNameValue.Value)
local cash = player.leaderstats:WaitForChild("Cash")
local Purchased1 = script.Parent.Parent.Parent.Frame.Item1.Purchased
local Purchased2 = script.Parent.Parent.Parent.Frame.Item2.Purchased
local Purchased3 = script.Parent.Parent.Parent.Frame.Item3.Purchased
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents
local Event = RemoteEvents.Buy

	Event.OnClientEvent:Connect(function()
		if cash.Value < price.Value then
			print("NotEnough")
			game.ReplicatedStorage.NotEnough:Clone().Parent = player.PlayerGui
			wait(3)
			player.PlayerGui.NotEnough:Destroy()
		end
			if cash.Value >= price.Value then
			cash.Value = cash.Value - price.Value
			if price.Value == 100 then
				local oldGun = player.Backpack:FindFirstChildOfClass("Tool") or player.Character:FindFirstChildOfClass("Tool")
				oldGun:Destroy()
				game.ReplicatedStorage.Tools.Pistol:Clone().Parent = player.Backpack
				Purchased1.Value = 1
			end
					if price.Value == 300 then
				local oldGun = player.Backpack:FindFirstChildOfClass("Tool") or player.Character:FindFirstChildOfClass("Tool")
				oldGun:Destroy()
				game.ReplicatedStorage.Tools.Revolver:Clone().Parent = player.Backpack
				Purchased2.Value = 1
					end
					if price.Value == 500 then
				local oldGun = player.Backpack:FindFirstChildOfClass("Tool") or player.Character:FindFirstChildOfClass("Tool")
				oldGun:Destroy()
				game.ReplicatedStorage.Tools.Uzi:Clone().Parent = player.Backpack
				Purchased3.Value = 1
			end
		end	
	end)

This is the RemoteEventsScript in serverscriptService

game.Players.PlayerAdded:connect(function(player)
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents
local Event = RemoteEvents.Buy
local Buy = player.PlayerGui:WaitForChild("Gui").Main.Info.Buy
	Buy.MouseButton1Click:Connect(function()
		Event:FireClient(player)
		print("Worked")
	end)
end)

2 comments

0

My bad, i answered that its because you are not loading the value upon joining, but i just didnt see the "View all 41 lines" tab lol i dont usually come on this site. Could it be because your script tries to load and input the data before the intvalue is generated? *edit

0

he trys to do server stuff with local script

1 answer

All of your attempts to change the value should be on the server. You should be firing a remote event/remote function(if you want a gui response, go with remote function), then checking if they have the correct amount and subtracting the value on the server.

You also should be cloning the tool to the player on the server as well so it replicates for other players.

This should help you: Remote functions/events

5 comments

0

There is also a broplem with closing the game right he is not binding to close..

0

That's not what they are asking. Bind to close is for shutdown, and i would use that for a soft shutdown.

0

CoolBoy how would i set up the remote event

0

My values are still not saving

0

When they purchase things fire a remote event and subtract the values/give them the item on the server.

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