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

Leaderstats With DataStore Failure?

Asked by 4 years ago

When a value incercrases, it doesn't saves with DataStore, here is the script:


local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore") -- Creates a leaderboard that shows player variables local function onPlayerJoin(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local gold = Instance.new("IntValue") gold.Name = "Gold" gold.Value = 0 gold.Parent = leaderstats local items= Instance.new("IntValue") items.Name = "Molecules" items.Value = 0 items.Parent = leaderstats local spaces = Instance.new("IntValue") spaces.Name = "Spaces" spaces.Value = 2 spaces.Parent = leaderstats end -- Run onPlayerJoin when the PlayerAdded event fires game.Players.PlayerAdded:Connect(onPlayerJoin) local data local success, errormessage = pcall(function() myDataStore:GetAsync(player.UserId.."-items") myDataStore:GetAsync(player.UserId.."-spaces") myDataStore:GetAsync(player.UserId.."-gold") end) game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() data = myDataStore:SetAsync(player.UserId.."-",player.leaderstats.gold.Value) data = myDataStore:SetAsync(player.UserId.."-",player.leaderstats.items.Value) data = myDataStore:SetAsync(player.UserId.."-",player.leaderstats.spaces.Value) end) if success then gold.Value = data items.Value = data spaces.Value = data else print("Unexcepted Error Happened while saving your data! ") warn(errormessage) end if success then print ("playerdatasaved") else print ("playerdatanotsaved") warn(errormessage) end end)
0
Does any of "print()" actually print something? KrzysiekRoblox 50 — 4y
0
Yes NerfLambdaWarrior120 3 — 4y
0
This wont solve your issue but you can set the parent of an instance as the second argument of the instance constructor. E.g: local myPart = Instance.new("Part", workspace) SuchASaltyLemon 35 — 4y
0
So, if yes, which one prints something? KrzysiekRoblox 50 — 4y
View all comments (3 more)
0
lemme check NerfLambdaWarrior120 3 — 4y
0
it doesn' t prints something (i confused with the script of the BuyButton that i putted in the game) NerfLambdaWarrior120 3 — 4y
0
here you have the link of the game (wich is uncopylocked and it it's just a test for the script for the original one) LINK: https://www.roblox.com/games/5023928146/Untitled-Game NerfLambdaWarrior120 3 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

First things first, "player" in lines 35-37 is not defined anywhere. Script doesn't understand what 'player' is.

Second, "gold", "items" and "spaces" in lines 52-54 are also not defined. Even if you create a variable in function, it doesn't work outside it.

I think your problem is, that you don't understand how to use scopes. Here's a Roblox tutorial about it: https://developer.roblox.com/en-us/articles/Scope

Also, there's a nice Roblox tutorial that explains how to correctly save player's data on exit: https://developer.roblox.com/en-us/articles/Saving-Player-Data

Making it the way you've done it is a bad practice. After player leaves the game script can't access "leaderstats" object, because it's already gone. You should do this the way as it's shown in the second link :)

And also, when you test DataStore, I recommend doing it in actual game, not in Roblox Studio. DataStore in Roblox Studio doesn't always work with game.Players.PlayerRemoving.

Hope I helped :)

Ad

Answer this question