Scripts work in Roblox Player but not Roblox Studio

Hello.

So I’m making a tycoon from my own recollection but I’ve recently noticed that my loader script only works in the roblox player and not studio. Its not really an issue, so long as it works in roblox player however, I am really curious why it doesn’t work in studio. The loader script works 100% of the time in roblox player.

Here’s the loader script. Just a simple check of a value, and if it is equal to 1 it starts a function in a module script.

local alreadyPurchased = require(game:GetService("ReplicatedStorage").alreadyPurchased)
local purchaseValue = game.ReplicatedStorage.purchaseStatus.purchaseStatus

local player = game.Players.LocalPlayer

game.Players.PlayerAdded:Connect(function()
	wait(1)
	print("loading")
	if purchaseValue.Value >= 1 then
		alreadyPurchased()
		print("success")
	end
end)

My client usage is crazy in studio as well, almost 1700 MB whereas roblox player barely uses 250 MB.

Maybe somehow the datastore is not loading the int value in time, so the loader script is playing before the value even loads?

Thanks for the read, any replies will be appreciated.

Edit - I’ve just realised that the loader script is being run before the datastore script, meaning, that I might have to put the loader script in a modulescript and run it from the datastore script or just put the script code inside of the datastore script.

1 Like

This event does not work as expected in solo mode , because the player is created before scripts that connect to PlayerAdded run. To handle this case, as well as cases in which the script is added into the game after a player enters, create an OnPlayerAdded function that you can call to handle a player’s entrance.

Source.

1 Like

Yup, just solved it by placing the loading script inside the datastore script.

1 Like