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

Leaderstats With DataStore Failure?

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)

7 comments

0

Does any of "print()" actually print something?

0

Yes

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)

0

So, if yes, which one prints something?

0

lemme check

0

it doesn' t prints something (i confused with the script of the BuyButton that i putted in the game)

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

1 answer

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 :)

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