Argument become nil because no reason and part.Touched:Connect(Function()) is spamming trigger


hi again, so i wann make a shop that will open if touching a part, but its bugged like its spamming trigger and argument become nil because no reason
here is the explorer
image
Screenshot 2025-01-11 155405

her is the script inside the part

script.Parent.Touched:Connect(function(part)
	if istouching == false then
		local character = part.Parent
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		print(player)
		player.PlayerGui.shop.LocalScript.turnonshop:FireClient(player)
		print("enter")
		task.wait(0.6)
		istouching = true
	end
end)

script.Parent.TouchEnded:Connect(function(part)
	if istouching == true then
		local character = part.Parent
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		player.PlayerGui.shop.LocalScript.turnoffshop:FireClient(player)
		print("leave")
		task.wait(0.5)
		istouching = false
	end
end)

here is the script inside shop gui

	local frame : Frame = script.Parent.Frame
	frame.Visible = true
	frame.Position = UDim2.new(0.122, 0, 0.988, 0)
	frame.Size = UDim2.new(0, 0, 0, 0)
	frame:TweenSizeAndPosition(UDim2.new(0, 809, 0, 491), UDim2.new(0.123, 0, 0.022, 0), nil, nil, 0.5)
end)

script.turnoffshop.OnClientEvent:Connect(function()
	local frame : Frame = script.Parent.Frame
	frame:TweenSizeAndPosition(UDim2.new(0, 0, 0, 0), UDim2.new(0.122, 0, 0.988, 0), nil, nil, 0.5)
	task.wait(0.5)
	frame.Visible = false
end)

your using is touching as a debounce right?.. you want to turn it to true right at the top…#

local debounce = false
script.Parent.Touched:Connect(function(part)
	if debounce == false then
		*debounce = true*
		local character = part.Parent
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		print(player)
		player.PlayerGui.shop.LocalScript.turnonshop:FireClient(player)
		print("enter")
		task.wait(0.6)
		*debounce = false*
	end
end)

script.Parent.TouchEnded:Connect(function(part)
	if debounce == true then
		*debounce = false*
		local character = part.Parent
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		player.PlayerGui.shop.LocalScript.turnoffshop:FireClient(player)
		print("leave")
		task.wait(0.5)
		*debounce = true*
	end
end)

Now as soon as a part is touched debounce is set to true and this stops the script from running multiple times

Basically Touched etc fire every frame where the character is touching the part. We just want to run this script on the first occurrence not every occurrence, and remember to turn the debounce off too…

1 Like


its still happen