Require Datastore2 blows leaderstats in Studio

OK, so this used to work a couple of weeks ago. I have been tracking this down for the last two weeks. I finally have the bug isolated and clearly identified. Yeah!!!

Summary: If I require datastore2, the leaderstats will not load in studio. I can load and run it in production with no problem, but can not run a test in studio. To recreate. run the code and no problems. Uncomment out the “–local DataStore2 = require(1936396537)” and leaderstats will not load.

--local DataStore2 = require(1936396537)

-- In 'ServerScriptService', add this into a script named 'PlayerSetup'
local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	-- Display an 'IntValue' on leaderboard
	local kills = Instance.new("IntValue")
	kills.Name = "KOs"
	kills.Value = 0
	kills.Parent = leaderstats

	local deaths = Instance.new("IntValue")
	deaths.Name = "Wipeouts"
	deaths.Value = 0
	deaths.Parent = leaderstats

	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value = 0
	level.Parent = leaderstats

end
 
-- Run 'onPlayerJoin()' when the 'PlayerAdded' event fires
game.Players.PlayerAdded:Connect(onPlayerJoin)

The DataStore2 Module specifies for it to work, you must have a boolvalue in ServerStorage named “SaveInStudio” and select it as true.

Yes, I have that. Still does not work.

I recommend taking a look at the DataStore2 Post , as this would for sure be helpful!

Are you in a Team Create server, by any chance? I just experienced this problem yesterday. If you’re working in a Team Create and you’re not the game’s owner (and not in a developer role if the game is owned by a group), you won’t get DataStore API access when using Play Solo or starting a server. Instead, you need to use Team Test.

I am the sole owner. No team development enabled. I am running Mac OS 10.14.6. Does the code code run on a PC ok?

Yeah, works fine on PC. Not sure what your issue is if it’s not a Team Create.

1 Like

Thanks so much. This could point to a possible security issue with my Mac. Studio may have some issue connecting to a live stream. The browser has permissions, but the studio app may not. I’ll check if that is the issue next. There was an update recently for Mac OS, that could actually be my culprit.

Thanks again.

I think the leaderstats isn’t working because it’s being connected too slowly. I think the datastore2 is taking maybe like a tenth of a second to require, and since lua code is synchronous, it yields the code and the play solo player joins before the onplayerjoin function is connected to the event. What you might be able to do is put it in a coroutine, which should run the code asynchronously.

Try replacing the top with

local Datastore2
local a = coroutine.wrap(function()
Datastore2 = require(1936396537)
end)

I tried it and that didn’t do the trick. I’ll just have a separate script without datastore2 references I use in studio to create my player folders and variables with dummy values and disable that before I publish. That way I can still test most of the functionality.

Thanks for trying.

Oh sorry, I just realized I forgot a small line.

local Datastore2
local a = coroutine.wrap(function()
Datastore2 = require(1936396537)
end)
a() -- forgot to call the coroutine

Does this work?

spawn(function()
  local DataStore2 = require(1936396537)
end)

-- In 'ServerScriptService', add this into a script named 'PlayerSetup'
local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	-- Display an 'IntValue' on leaderboard
	local kills = Instance.new("IntValue")
	kills.Name = "KOs"
	kills.Value = 0
	kills.Parent = leaderstats

	local deaths = Instance.new("IntValue")
	deaths.Name = "Wipeouts"
	deaths.Value = 0
	deaths.Parent = leaderstats

	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value = 0
	level.Parent = leaderstats

end
 
-- Run 'onPlayerJoin()' when the 'PlayerAdded' event fires
game.Players.PlayerAdded:Connect(onPlayerJoin)

This is probably happening because of how you’re using the PlayerAdded event. What’s happening is the player is added before that script finishes loading, so the event never fires.

You can call the onPlayerJoin function in for loop to fix this.

See this post for more information:

6 Likes

Seems to be working, I can see my errors popping up inside my code now. Yeah!!!

OMG, now not working. Identify wall, hit head on wall, repeat until unable. I’ll play with this a little longer. I do appreciate all of the input. I know I’m getting closer to a working solution.