Function adds more coins than what it is supposed to

I’m making this thing where I’m supposed to click whether it’s naughty or nice in this christmas game. But it’s supposed to only add 1 but it adds more than 1 every round. I thought it was because it was playing the function multiple times and I added returns but it still doesn’t work. Here is my code:

local non = script.Parent.ScreenGui.Non
local prompt
local nice
local player = game.Players.LocalPlayer

click.MouseClick:Connect(function()
	Play()
end)

function Play()
	script.Parent.ScreenGui.Non.Visible = true
	prompt = math.random(1, 6)
	if prompt == 1 then
		non.Promptnon.Text = "He kicked somebody's dog"
		nice = false
	end
	if prompt == 2 then
		non.Promptnon.Text = "He bullies"
		nice = false
	end
	if prompt == 3 then
		non.Promptnon.Text = "He punched his brother"
		nice = false
	end
	if prompt == 4 then
		non.Promptnon.Text = "He likes dogs"
		nice = true
	end
	if prompt == 5 then
		non.Promptnon.Text = "He doesn't bother anybody"
		nice = true
	end
	if prompt == 6 then
		non.Promptnon.Text = "He helps people"
		nice = true
	end
	non.Nice.MouseButton1Click:Connect(function()
		if nice == true then
			player.Leaderstats.Coins.Value += 1
			Play()
			return
		else
			Play()
			return
		end
	end)
	non.Naughty.MouseButton1Click:Connect(function()
		if nice == false then
			player.Leaderstats.Coins.Value += 1
			Play()
			return
		else
			Play()
			return
		end
	end)
end```
1 Like

You are calling the Play() function again in the event functions which causes the Play function to register more event functions which causes your problem.

Remove the Play() call from both event functions and your problem should be solved.

1 Like

I was messing with it and roblox studio crashed and I forgot to save so I’m gonna remake everything and I will tell you if it fixes it

2 Likes

Ok I’m done but I want it to go again after you pick nice or naughty. Do you know how to make that?

1 Like

Ok I was able to fix it thank you!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.