Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 4 years ago

--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)
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 Scriptulus 30 — 5y
0
he trys to do server stuff with local script User#27824 0 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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

0
There is also a broplem with closing the game right he is not binding to close.. Luka_Gaming07 534 — 4y
0
That's not what they are asking. Bind to close is for shutdown, and i would use that for a soft shutdown. coolboy6786543 113 — 4y
0
CoolBoy how would i set up the remote event GunzerkingBeast21 -4 — 4y
0
My values are still not saving GunzerkingBeast21 -4 — 4y
0
When they purchase things fire a remote event and subtract the values/give them the item on the server. coolboy6786543 113 — 4y
Ad

Answer this question