Script fires event multiple times, for no reason

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I got a script that fires a event that gives you a point, so when you fire it once, it gives you one point.
  2. What is the issue? Include screenshots / videos if possible!

The issue is that when I fire it in my script, it gives me like 7 points, which means it fires it 7 times for no reason. I am utilizing it in another script, for another ui, and it works perfectly there.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

The script that fires it: The event is at the bottom.

local order = {}
local click = true
local orderer = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
 if click and script.Parent.Parent.ListAmount.Value >= 1 and script.Parent.Parent.CustomerServe.Text ~= '' and game.Players:FindFirstChild(script.Parent.Parent.CustomerServe.Text) then
    script.Parent.Parent.Parent.Parent.Tablets.OpenMenu.Value = 'false'
	click = false

	for i, x in pairs(script.Parent.Parent.Order:GetChildren()) do
		if x:IsA("Frame") then
			table.insert(order,x.Name)
		end
	end

print(unpack(order));
wait(1)
game.ReplicatedStorage.Tablets:FireServer(order,script.Parent.Parent.TableNum.Value,script.Parent.Parent.CustomerServe.Text)

for i, x in pairs(script.Parent.Parent.Order:GetChildren()) do
	if x:IsA("Frame") then
		x:Destroy()
	end
end
for k in pairs (order) do
    order[k] = nil
end
  script.Parent.Parent.ListAmount.Value = 0
  script.Parent.Parent.TableNum.Value = ''
-----------------------------------------------------------------------------------

    script.Parent.Parent.CustomerServe.Text = ''
		for i, x in pairs(script.Parent.Parent:GetChildren()) do
			if x:IsA("ScrollingFrame") and x.Name ~= 'Display' or x:IsA("ScrollingFrame") and x.Name ~= 'Order' then
				x.Visible = false
				script.Parent.Parent:TweenPosition(UDim2.new(0.205, 0, 1.25, 0),"In", "Linear",1.0,true)
				wait(1)
				game.ReplicatedStorage.GivePoint:FireServer()
				wait(0.1)
				script.Parent.Parent.Visible = false
------------------------------------------------------------------------------------------
click = true
wait(script.Parent.Parent.OrderCoolDown.Value) 
		script.Parent.Parent.Parent.Parent.Tablets.OpenMenu.Value = 'true'
			end
		end
	end
end)

The event script, that tells what the event is for: Remember this connects other events too.

	game.Players.PlayerAdded:Connect(function(Player)
	local Stat = Instance.new("NumberValue",Player)
	Stat.Name = 'Stats'
	local Stat2 = Instance.new("NumberValue",Stat)
	Stat2.Name = 'OrderClaim'
	local DataStoreService = game:GetService("DataStoreService")
	local PointsStore = DataStoreService:GetDataStore("PointsStore")
	local SavedPoints = PointsStore:GetAsync(Player.UserId)
    local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"	
	local PointsValue = Instance.new("IntValue", Leaderstats)
	PointsValue.Name = "Points"
	if SavedPoints ~= nil then
	PointsValue.Value = SavedPoints
	PointsValue.Changed:Connect(function(NewPoints)
	PointsStore:SetAsync(Player.UserId, NewPoints)
		end)
	end
end)
game.ReplicatedStorage.Claim.OnServerEvent:Connect(function(player,id)
	player.Stats.OrderClaim.Value = 1
	game.ReplicatedStorage.Claim:FireAllClients(id)
end)
game.ReplicatedStorage.Claim2.OnServerEvent:Connect(function(player)
	player.Stats.OrderClaim.Value = 0
end)
game.ReplicatedStorage.GivePoint.OnServerEvent:Connect(function(player)
	player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1
end)

Try adding debounces to your server script? From what I understand in the local script, it’s attempting to find all the correct arguments in the GetChildren() function, and the or statement might be your issue there if it’s firing multiple times at once

So you are firing the event once for every ScrollingFrame in script.Parent.Parent?

This line in particular might be a problem. I’m assuming that you want it to fire only on ScrollingFrames that are not named Display and not named Order. Currently it fires on everything that is a ScrollingFrame. To fix it, you would write it like this.
if x:IsA("ScrollingFrame") and x.Name~='Display' and x.Name~='Order' then

1 Like

Thank you! I see what you mean by it firing the event multiple times, and I tried ur fix and it didn’t work but you gave me a good brainer. The fact that the ScrollingFrames fires, I just moved the end, to it ends the expression after that and then fires the point event and it fixed it! Only problem is that it takes about 2 seconds for the point to come in but that’s fine!

Blockquote

script.Parent.Parent.CustomerServe.Text = ''
	for i, x in pairs(script.Parent.Parent:GetChildren()) do
		if x:IsA("ScrollingFrame") and x.Name ~= 'Display' or x:IsA("ScrollingFrame") and x.Name ~= 'Order' then
			x.Visible = false
			script.Parent.Parent:TweenPosition(UDim2.new(0.205, 0, 1.25, 0),"In", "Linear",0.5,true)
			wait(0.5)
			script.Parent.Parent.Visible = false
		end
	end	
end
game.ReplicatedStorage.GivePoint:FireServer()	

click = true
wait(script.Parent.Parent.OrderCoolDown.Value)
script.Parent.Parent.Parent.Parent.Tablets.OpenMenu.Value = ‘true’
end)

Blockquote

1 Like