Help with shop Gui

Hello, I am making a game with a shop and I have ran into a road bump.
I need to make Multiple shops but my script wont allow for it

RunService.Heartbeat:Connect(function()
	for Index,Shop in pairs(ShopData) do
		local Distance = (HumanoidRootPart.Position - FunctionParts[Index].Position).Magnitude
		if Distance < 6 then
			if not Debounce then
				print("eeee")
				Debounce = true
				SelectedShop = Shop
				Camera.CameraType = Enum.CameraType.Scriptable
				ShopUIFolder.ShopFrame:TweenPosition(
					UDim2.new(0.5, 0,.5, 0),
					Enum.EasingDirection.InOut,
					Enum.EasingStyle.Sine,
					.4,
					true

				)
				SelectedItem = Shop.Swords[IndexNumber]
				PriceUI.Text =  Shop.Swords[IndexNumber].Price.Amount
				NameUI.Text = Shop.Swords[IndexNumber].Name
				Tween(Camera, Shop.Swords[IndexNumber].Object.PrimaryPart)
			end
		else
			if Debounce then
				Camera.CameraType = Enum.CameraType.Custom
				Debounce = false
				IndexNumber = 1
				ShopUIFolder.ShopFrame:TweenPosition(
					UDim2.new(0.5, 0,-1, 0),
					Enum.EasingDirection.In,
					Enum.EasingStyle.Back,
					.4,
					true
				)
			end
		end
	end
end)

I want the script to only print eeee once whenever it gets to a shop. Since there are other shops that aren’t in range the debounce keeps being set back to false and it spams eeee. Does anyone know a way i can solve this? Thanks.

2 Likes

Try this:

local function GetClosestShop()
	local ClosestShop
	local Distance = 6
	for Index,Shop in pairs(ShopData) do
		local mag = (HumanoidRootPart.Position - FunctionParts[Index].Position).Magnitude
		if mag <= Distance then
			Distance = mag
			ClosestShop = Shop
		end
	end
	return ClosestShop
end

RunService.Heartbeat:Connect(function()
	local Shop = GetClosestShop()
	
	if Shop then
		if Shop ~= SelectedShop then
			SelectedShop = Shop
			Camera.CameraType = Enum.CameraType.Scriptable
			ShopUIFolder.ShopFrame:TweenPosition(
				UDim2.new(0.5, 0,.5, 0),
				Enum.EasingDirection.InOut,
				Enum.EasingStyle.Sine,
				.4,
				true

			)
			SelectedItem = Shop.Swords[IndexNumber]
			PriceUI.Text =  Shop.Swords[IndexNumber].Price.Amount
			NameUI.Text = Shop.Swords[IndexNumber].Name
			Tween(Camera, Shop.Swords[IndexNumber].Object.PrimaryPart)
		end
	else
		if SelectedShop then
			SelectedShop = nil
			Camera.CameraType = Enum.CameraType.Custom
			IndexNumber = 1
			ShopUIFolder.ShopFrame:TweenPosition(
				UDim2.new(0.5, 0,-1, 0),
				Enum.EasingDirection.In,
				Enum.EasingStyle.Back,
				.4,
				true
			)
		end
	end
end)
1 Like

instaed of distance < 3 do
if Distance <= 6 then

1 Like

This is what your script would look like now

RunService.Heartbeat:Connect(function()
	for Index,Shop in pairs(ShopData) do
		local Distance = (HumanoidRootPart.Position - FunctionParts[Index].Position).Magnitude
		if Distance <= 6 then
			if not Debounce then
				print("eeee")
				Debounce = true
				SelectedShop = Shop
				Camera.CameraType = Enum.CameraType.Scriptable
				ShopUIFolder.ShopFrame:TweenPosition(
					UDim2.new(0.5, 0,.5, 0),
					Enum.EasingDirection.InOut,
					Enum.EasingStyle.Sine,
					.4,
					true

				)
				SelectedItem = Shop.Swords[IndexNumber]
				PriceUI.Text =  Shop.Swords[IndexNumber].Price.Amount
				NameUI.Text = Shop.Swords[IndexNumber].Name
				Tween(Camera, Shop.Swords[IndexNumber].Object.PrimaryPart)
			end
		else
			if Debounce then
				Camera.CameraType = Enum.CameraType.Custom
				Debounce = false
				IndexNumber = 1
				ShopUIFolder.ShopFrame:TweenPosition(
					UDim2.new(0.5, 0,-1, 0),
					Enum.EasingDirection.In,
					Enum.EasingStyle.Back,
					.4,
					true
				)
			end
		end
	end
end)
1 Like

Thanks for your input. I remade the script before looking at these replies and it worked. This is almost exactly what I did. Thanks! Sorry for not responding sooner!

1 Like