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

data store service wont work?

Asked by
DJH_100 28
4 years ago

i have a script,

require(script:WaitForChild('Coins'))

and then i have a module script inside ,

local module = {} local DataStore = game:GetService("DataStoreService"):GetDataStore("Tutorial3816310153")

local StartingLevel = 0

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

local Stats = plr:FindFirstChild("leaderstats")

local Level = Stats.Coins

local SavedLevel = DataStore:GetAsync(plr.userId .. "-Coins") if SavedLevel ~= nil then Level.Value = SavedLevel end

end)

game.Players.PlayerRemoving:connect(function(plr) local id = plr.userId local LevelValue = plr.leaderstats.Coins.Value DataStore:SetAsync(id.."-Coins", LevelValue) end)

return module

BuT It wOn'T WoRk

0
Which part does not work, obtaining the value or saving the value? Also if the output throws any errors please add them aswell User#834 0 — 4y
0
i does not have an error but it wont work DJH_100 28 — 4y

1 answer

Log in to vote
2
Answered by 4 years ago

I am making a simple DataStore that saves you money in leaderstats. It isn’t saving for some reason but it is loading fine and I can’t work out why.

Here is the code: Server Script in ServerScriptService:

local ReplicatedStorage = game:GetService("ReplicatedStorage") local Remotes = ReplicatedStorage:WaitForChild("Remotes") local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("StatsSave")

local DefultData = { Money = 100;
} -- Load Data game.Players.PlayerAdded:Connect(function(Player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = Player

local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Parent = leaderstats

local Data = DataStore:GetAsync(Player.UserId)

if Data then
    Money.Value = Data
else
    Money.Value = DefultData.Money
end

end)

-- Save Data local function SaveData(Player) local Money = Remotes.MoneySave:InvokeClient(Player) DataStore:SetAsync(Player.UserId, Money) end

game.Players.PlayerRemoving:Connect(SaveData)

LocalScript in StarterPlayerScripts:

local leaderstats = script.Parent.Parent:WaitForChild("leaderstats") local ReplicatedStorage = game:GetService("ReplicatedStorage") local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local function SendData() return leaderstats.Money.Value end

Remotes.MoneySave.OnClientInvoke = SendData I have a RemoteFunction in ReplicatedStorage in a folder called Remotes and the RemoteFunction is called MoneySave.

Note:I have Studio API Services turned on and I get no errors.

Thank you in advanced.

Ad

Answer this question