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

DataStore Only works in Roblox studio?

Asked by 5 years ago
Edited 5 years ago

Trying to store PlayerGui.Menu.Stats.Z.Money.Text

localScript position = game.Players.LocalPlayer.PlayerGui.Menu.Stats.Z.Money.LocalScript

local money = script.Parent:WaitForChild("Money")
local Gui = script.Parent

money.Changed:connect(function()
    Gui.Text = money.Value
end)

ServerScriptService

local dataStore = game:GetService("DataStoreService"):GetDataStore("Money")

function SaveData(dataname, playerid, value)
    dataStore:SetAsync(playerid, value)
print(playerid)
print(value)
end

game.Players.PlayerAdded:connect(function(player)

    local money = player.PlayerGui:WaitForChild("Menu").Stats.Z.Money["Money"]
     money.Value = dataStore:GetAsync(player.userId)


        money.Changed:connect(function()
         SaveData("Money", player.userId, money.Value)
wait(1)
SaveData("Money", player.userId, money.Value)
    end)
end)
0
You have FireServer in a server script? User#5423 17 — 5y
0
No it's not Hypoxic1 8 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

If you want to save your currency into the datastore, I suggest that you use an IntValue or NumberValue and put in the LocalPlayer. Here's the code to create it :

Creating Currency (ServerScript) :

-- Example code only change what you want.

local dataStore = game:GetService("DataStoreService")
local moneyData = game:GetService("DataStoreService"):GetDataStore("Money")

function SaveData(dataname, playerid, value)
    dataStore:GetDataStore(dataname):SetAsync(playerid, value)
end

game.Players.PlayerAdded:connect(function(player)

    -- Optional you can put in outside of leaderstats too
    local stats = Instance.new("Folder")    
    stats.Name = "leaderstats"
    stats.Parent = player

    -- You can also change the money.Parent to "player" (No double quotes)
    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = moneyData:GetAsync(tostring(player.userId)) or 350
    money.Parent = stats

    while true do
        wait(.5)
        money.Changed:connect(function()
            SaveData("Money", player.userId, money.Value)
        end)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    SaveData("Money", player.userId, player.leaderstats.Money.Value)
end)

With this you can only edit the values in-game.

Putting the currency into the gui (LocalScript) :

-- Example code only change what you want.

local playerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local mainGui = playerGui:WaitForChild("MainGui")
local stat = game.Players.LocalPlayer:WaitForChild("leaderstats")
local money = stat:WaitForChild("Money")

local moneyGui = script.Parent.Money

moneyGui.Text = "$" .. money.Value

money.Changed:connect(function()
    desiredMoney.Value = money.Value
end)

Sorry that I don't use RemoteEvents or RemoteFunction for this method.

0
I edited the question tell me what i have done wrong please, it's not working. Hypoxic1 8 — 5y
0
Nvm I fix it Hypoxic1 8 — 5y
0
Now it only works in studio -_- Hypoxic1 8 — 5y
0
Have you checked the "Save Place API" to true in the "Configure Place"? Here's the image on it, you need to configure that : https://imgur.com/a/kq6pRDQ aditya200 3 — 5y
Ad

Answer this question