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

Why does this leaderstats script not work? (Explanation exceeded the title character limit)

I have a script where, if a block is touched, all of the leaderstats reset to 0 except for Money. Money's value is supposed to become the quotient of the value of Views divided by 1000. The resetting of the values part works fine, but Money's value stays at 0. Does anyone know why? Since I don't know how to add those code block things I'll link a screenshot of my code here: http://prntscr.com/hv82hi You'll notice that in the screenshot some of the code on line 4 is cut off. After player.leaderstats.Views it says .Value / 1000.

1 comment

0

To use a code block: click on the Code block button then paste your code in between the two lines of "~~~".

2 answers

✓ Accepted answer

Your full code:

script.Parent.Touched:Connect(function()
 	for _, player in pairs(game.Players:GetPlayers()) do
		if player:FindFirstChild("leaderstats") then
			player.leaderstats.Money.Value = player.leaderstats.Money.Value + players.leaderstats.Views.Value / 1000
			player.leaderstats.Views.Value = 0
			player.leaderstats.Subscribers.Value = 0
			player.leaderstats.Videos.Value = 0
		end
	end
end)

One of the issues I can think of is that you might be dividing a 0 since you set Views to 0. A zero divided by any number is obviously 0. This is probably happening because you don't have a cool down of when the player can touch the part.

Try this following code or make sure what you are adding doesn't come out to be a 0 then divide it.


local debounce = true --Search debounces on roblox wiki if you don't know about it

script.Parent.Touched:Connect(function()
 	for _, player in pairs(game.Players:GetPlayers()) do
		if player:FindFirstChild("leaderstats") and debounce then --Debounce check
			debounce = false --turn off debounce

			player.leaderstats.Money.Value = player.leaderstats.Money.Value + players.leaderstats.Views.Value / 1000
			player.leaderstats.Views.Value = 0
			player.leaderstats.Subscribers.Value = 0
			player.leaderstats.Videos.Value = 0

			
			wait(3) --Our set cooldown
			debounce = true --Turn on the debounce to allow the player to touch again
		end
	end
end)

1 comment

0

Thank you, it worked!

Hello, invite me to your game and I will fix your problem. I don't know how to exactly explain it, but I'll explain it the best I can.

3 comments

0

The game is still in development.

0

This answer isn't a solution so it should stay in the comments.

0

Well, I can still help out, but I could use all help that's needed if you invite me.

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