I believe that I know that answer to your question.
Roblox studio runs much faster than roblox, which is commonly why most errors that occur is "works in studio but not online", as such in this case aswell. Your problem is that you used if player:FindFirstChild("leaderstats") then which means if it can't find the leaderstats, it'll end without running the code inside. To "patch" this you could add another method to it eg
if player:FindFirstChild("leaderstats") then
elseif player:FindFirstChild("leaderstats") == nil then
player:WaitForChild("leaderstats")
end
WaitForChild is basically a short way of doing something like:
local parent = script
repeat wait() until parent:FindFirstChild("leaderstats")
EDIT START:
Forgot to explain why to do that. What this script does now is checks if the player has leaderstats, if they don't, instead of ending the script and having to wait another 120 seconds, it goes to the elseif part of the script, where it wait until the player has leader stats
EDIT END:
After saving this and testing this in online mode, if it once again doesn't work, go into Developer Console (press f9). If the script didn't work, there should be a warning: "Infinite yield possible ... " etc
This could mean that the leaderstats aren't in the player, or are in a folder inside the player. I suggest getting this explorer and property script made by a former roblox intern. You can get it by clicking here
Whenever you encounter a problem in studio, I find it very helpful to use this, find out if what I want is actually there, and then remove it. This script has to be put into ServerScriptService to work, and instead of editing the settings, I advise just using it when needed and deleting it when not, as sometimes it may glitch and allow all users to view it. That's a problem as it allows hackers to find directories and edit things via Script Executor (Level 7 hack)
I hoped this helped. If you need any more help just contact me.
2 comments
Make this a localscript and put it in startergui, you cannot use game.Players.LocalPlayer in the real game from a regular script or anything with the player in a regular script but you can with a localscript
This script does not have game.Players.LocalPlayer in it