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 Roblox game? [PLZ HELP]

Please help. This is like the fourth time I've posted this question and twice it has been wrongfully moderated by this joke of a moderation team and 0 of the times the question has correctly been answered. (If this is taken down before it has been answered it will be reposted. Just sayin) Anyway. 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. Better to be safe than sorry. 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
end)

1 answer

It can be that "leaderstats" isn't loaded in the Player when the loop first starts. After that, it has to wait 120 seconds for the next loop.

You can use PlayerAdded to copy a template of the leaderstats and insert it to the player. After that the loop would run and check if the Value of their Subscribers is less that 10.

--The hierarchy of the leaderstats template I have inside the script:
	--leaderstats (folder)
		--Subscribers (IntValue)
		--Views (IntValue)
		--Videos (IntValue)

game.Players.PlayerAdded:Connect(function(player)
	local temp = script:WaitForChild("leaderstats"):Clone()
	temp.Parent = player
end)


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

As you can probably see in the for statement I had changed :GetChildren() to :GetPlayers() because :GetChildren() can also get anything else that is not a player. You should use :GetPlayers() when you ONLY want to do stuff to players.

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