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

How do I make my event listeners fire once?

I have a local script that acts as a controller for a minigame within my game. This minigame works by collecting "Microchips" in a limited amount of time. If the user collects a certain amount of "Microchips" within the given time (For testing purposes I've set it to 1), the user is given a 20 "Coin" bonus. The problem I have is that after the user has played the minigame for the first time (the first time the user plays the game the script functions as expected), if they were to play the minigame again, and click a "Yes" button in the ScreenGui to start the game, the event listener fires the code inside it twice, therefore giving the player a 40 Coin bonus. When the player plays for the third time the code within the event listener fires three times and thus the bonus is now 60 "Coins" and so on and so forth. I am fairly new to LUA programming and this probably is a simple fix that I am overlooking.

Here is the code within my local script :

local guiPressed = false
local frameVisible = false
local playing = false

workspace.TimeTrials.GUI.Touched:Connect(function(hit)

	if guiPressed == false then
		
		guiPressed = true
		
		local plrChar = hit.Parent
		local plr = game.Players.LocalPlayer
		local frame = plr.PlayerGui.TimeTrials:FindFirstChild("Frame")
		local mainText = frame.Bottom.Main
			
		local yes1 = frame.TopButtons.Yes1
		local no1 = frame.TopButtons.No1
			
		if frame.Position.Y.Scale >= 1 and playing == false then
			
			local frameVisible = true
			
			frame.Top.Visible = false
			frame.TopButtons.Visible = true
			
			mainText.Text = "Hey! Would you like to compete in my time trial? If you collect enough microchips, I'll give you a little gift!"
		
			frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 0.79, 0)) 
		
			yes1.Active = true
			no1.Active = true
		
			yes1.MouseButton1Click:Connect(function()
				
				playing = true
				
				frame.TopButtons.Visible = false
				frame.Top.Visible = true
				
				plrChar.HumanoidRootPart.Anchored = true
				plrChar.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-15.3, 20, 107.1))
				plrChar.HumanoidRootPart.Orientation = Vector3.new(0, 0, 0)
				
				mainText.Text = "Ok! Starting in 3"
				wait(1)
				mainText.Text = "Ok! Starting in 2"
				wait(1)
				mainText.Text = "Ok! Starting in 1"
				wait(1)
				
				local startMicrochips = plr.leaderstats.Microchips.Value
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 1.1, 0))
				
				plrChar.HumanoidRootPart.Anchored = false
				
				wait(5)
					
				local finalMicrochips = plr.leaderstats.Microchips.Value
				local collectedMicrochips = finalMicrochips - startMicrochips
			
				if collectedMicrochips == 1 then
					
					mainText.Text = "Time up! You collected " .. tostring(collectedMicrochips) .. " Microchip!"
					
				else
					
					mainText.Text = "Time up! You collected " .. tostring(collectedMicrochips) .. " Microchips!"
					
				end
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 0.79, 0))
				
				if collectedMicrochips > 1 then
					
					wait(2.5)
					
					mainText.Text = "Wow! You collected a lot of Microchips! Here are 20 Coins as a gift from me!"
					plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 20
					
				end
				
				wait(2.5)
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 1.1, 0))
				
				wait(1)
				
				playing = false
				frameVisible = false
				guiPressed = false
				
			end)
	
			no1.MouseButton1Click:Connect(function()
				
				frame.TopButtons.Visible = false
				frame.Top.Visible = true
				
				mainText.Text = "Ok, see you some other time!"
				
				wait(2)
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 1.1, 0))
				
				wait(1)
				
				frameVisible = false
				guiPressed = false
				
			end)
	
		end
				
	end
	
end)

wait(2.5)

Thanks in advance.

3 comments

0

u dont set the microship value back to nil,0 so everytime u start a new game u still got 1value so when u win again u got 2 so this might be one of the reasons it keeps stacking up every win

0

Btw dont add values in a local script u should do this on the server trough remote servers

0

Oh yea! Forgot about filtering enabled. Thanks :) However the way it calculates Microchips collected works and I don't think that it should interefere with the coins. Thanks anyways though! :D

1 answer

It seems that the issue prevailed. I've made it so that it fires a remote event to the server to give out the coins (This is so that the coins are given on the server side making them a global change and able to be saved by the datastore server script). However, the issue remained there.

Updated Local Script Inside The ScreenGui:

local guiPressed = false
local frameVisible = false
local playing = false

local remoteEvent = game.ReplicatedStorage.RemoteEvents.GiveCoins

workspace.TimeTrials.GUI.Touched:Connect(function(hit)

	if guiPressed == false then
		
		guiPressed = true
		
		local plrChar = hit.Parent
		local plr = game.Players.LocalPlayer
		local frame = plr.PlayerGui.TimeTrials:FindFirstChild("Frame")
		local mainText = frame.Bottom.Main
			
		local yes1 = frame.TopButtons.Yes1
		local no1 = frame.TopButtons.No1
			
		if frame.Position.Y.Scale >= 1 and playing == false then
			
			local frameVisible = true
			
			frame.Top.Visible = false
			frame.TopButtons.Visible = true
			
			mainText.Text = "Hey! Would you like to compete in my time trial? If you collect enough microchips, I'll give you a little gift!"
		
			frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 0.79, 0)) 
		
			yes1.Active = true
			no1.Active = true
		
			yes1.MouseButton1Click:Connect(function()

				playing = true
				
				frame.TopButtons.Visible = false
				frame.Top.Visible = true
				
				plrChar.HumanoidRootPart.Anchored = true
				plrChar.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-15.3, 20, 107.1))
				plrChar.HumanoidRootPart.Orientation = Vector3.new(0, 0, 0)
				
				mainText.Text = "Ok! Starting in 3"
				wait(1)
				mainText.Text = "Ok! Starting in 2"
				wait(1)
				mainText.Text = "Ok! Starting in 1"
				wait(1)
				
				local startMicrochips = plr.leaderstats.Microchips.Value
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 1.1, 0))
				
				plrChar.HumanoidRootPart.Anchored = false
				
				wait(5)
					
				local finalMicrochips = plr.leaderstats.Microchips.Value
				local collectedMicrochips = finalMicrochips - startMicrochips
			
				if collectedMicrochips == 1 then
					
					mainText.Text = "Time up! You collected " .. tostring(collectedMicrochips) .. " Microchip!"
					
				else
					
					mainText.Text = "Time up! You collected " .. tostring(collectedMicrochips) .. " Microchips!"
					
				end
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 0.79, 0))
				
				if collectedMicrochips > 1 then
					
					wait(2.5)
					
					mainText.Text = "Wow! You collected a lot of Microchips! Here are 20 Coins as a gift from me!"
					remoteEvent:FireServer(20)
					
				end
				
				wait(2.5)
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 1.1, 0))
				
				wait(1)
				
				playing = false
				frameVisible = false
				guiPressed = false
				
			end)
	
			no1.MouseButton1Click:Connect(function()
				
				frame.TopButtons.Visible = false
				frame.Top.Visible = true
				
				mainText.Text = "Ok, see you some other time!"
				
				wait(2)
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 1.1, 0))
				
				wait(1)
				
				frameVisible = false
				guiPressed = false
				
			end)
	
		end
				
	end
	
end)

wait(2.5)

New Server Side Event Listener:

local remoteEvent = game.ReplicatedStorage.RemoteEvents.GiveCoins

remoteEvent.OnServerEvent:Connect(function(plr, coinsGiven)
	
	plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + coinsGiven
	
end)

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