Why is this boost script not working?

Hey. I am a beginner scripter and I do not know why my script is not working. I want it to be so that every 50 seconds your gems will be up by one. I did not make the leaderstats I made the for I part.

Code:

function onPlayerEntered(newPlayer)
	wait(0.5)
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"

	local score = Instance.new("IntValue")

	score.Name = "Gems"
	score.Value = 0

	score.Parent = stats	
	stats.Parent = newPlayer
end

game.Players.ChildAdded:connect(onPlayerEntered)

for i = 1, 50 do
	wait(1)
	game.newPlayer.stats.Gems.Value = 1
end

Thanks!
1 Like

This does not appear to be the full script. Could you please send the entire script (in text)? And also, what are you trying to achieve, and are you getting any errors in the output?

1 Like

Fixed it, the output says there is an error on line 19. I want it to be so that every 50 seconds your gems will be up by one.

Why are you using ChildAdded instead of PlayerAdded?

I did not make the leaderstats script. I got it from the toolbox :flushed:

1 Like

Okay so to do that, you may want to do this in a separate script. First, let’s fix your current script. There are a few issues with your code.

Here is your fixed code:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player) -- fires when player joins the game
	local leaderstats = Instance.new("Folder") -- creates leaderstats folder
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	

	local score = Instance.new("IntValue") -- creates an IntValue
	score.Name = "Gems"
	score.Value = 0
	score.Parent = leaderstats	
end)
1 Like

Bit of a strange script you have a wait when creating the leaderstats.

The leaderstats is correct as it parents the instance last after all other values have been set.

1 Like

Yes it is strange, for some reasoned before I edited the script I spawned in with a noob character.

Pareting this to the game before the score is going to lead to additinal update changes. Best to parent it last.

1 Like

Now to what you have said, you want the Gem Value to increase by 1 every 50 seconds. To do this, you want to make a while do loop.

Here is your updated code (replace this code with the code I have sent before):

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local score = Instance.new("IntValue")
	score.Name = "Gems"
	score.Value = 0
	
	score.Parent = leaderstats	
	leaderstats.Parent = player
	
	while wait(50) do
		score.Value += 1
		if not player then
			break
		end
	end
end)
1 Like

This will run even after players is no longer in the game the game.

Yes, I agree with @BabyNinjaTime, you should absolutely make this in another script. After fixing your leaderstats script as in his code, make a separate LocalScript inside StarterPlayerScripts. Input this code inside the script:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local gems = player.leaderstats.Gems

while true do
task.wait(50)
gems.Value = gems.Value + 1
end

For some reason it isn’t working

Any errors in the output? What isn’t exactly working?

There are no errors in the output. It just stays at 0

This is a local change. This will not update the gem value on the server.

I am going to fix the code so that when you buy a dev product it will give you gems every second

Have you waited 50 seconds at least?

Will try now. But the code is supposed to give you one every second

edit: sounded kinda rude sorry I appreciate you guys helping

Oh, well above you have said that you want it to add up after 50 seconds. Here is ur fixed code:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local score = Instance.new("IntValue")
	score.Name = "Gems"
	score.Value = 0
	
	score.Parent = leaderstats	
	leaderstats.Parent = player
	
	while wait(1) do
		score.Value += 1
		if not player then
			break
		end
	end
end)
1 Like