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

Leaderstat value changing script works in Studio but not in my Roblox game?

This is one of my scripts. It is supposed to take the value of Videos and add it to the value of Views every 2 minutes. It works perfectly in Studio, but not the game it is on. If anyone wants to see the game it is on, here it is: https://web.roblox.com/games/1300877434/OPEN-Be-a-YouTuber-ALPHA#!/about I don't know if that will help answer my question or not, but just in case I put it there. Here is my script:

while wait(120) do
for _,player in pairs(game.Players:GetChildren()) do
if player:FindFirstChild("leaderstats")then
if player.leaderstats.Subscribers.Value <= 10 then
player.leaderstats.Views.Value = player.leaderstats.Views.Value + player.leaderstats.Videos.Value
end
end
end
end

The subscribers value is under 10 if you think that the problem is it being over ten.

2 comments

0

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

0

This script does not have game.Players.LocalPlayer in it

1 answer

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
	--code
elseif player:FindFirstChild("leaderstats") == nil then
	player:WaitForChild("leaderstats")
	--code
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.

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