Teleport to nearest sell area

how do I make it so that you can teleport to the nearest sell area,
image
there’s multiple sell areas called sellconsumables

local Players = game:GetService("Players")

local LocalPlayer: Player = Players.LocalPlayer

local leaderstats: Folder = LocalPlayer:WaitForChild("leaderstats") :: Folder
local slimeSize: NumberValue = leaderstats:WaitForChild("Slime Size") :: NumberValue
local maxSize: NumberValue = LocalPlayer:WaitForChild("maxSize") :: NumberValue

script.Parent.MouseButton1Click:Connect(function()
	if LocalPlayer.Character ~= nil and LocalPlayer.Character.PrimaryPart ~= nil then
		local hrp = LocalPlayer.Character.PrimaryPart
		local targetPosition = workspace.sellConsumables.Position
		hrp.CFrame = CFrame.new(targetPosition) * CFrame.Angles(0, hrp.Orientation.Y, 0)
	end
end)

local function update(): ()
	script.Parent.Visible = slimeSize.Value >= maxSize.Value * 100
end

update()

slimeSize.Changed:Connect(update)
maxSize.Changed:Connect(update)```
1 Like
script.Parent.MouseButton1Click:Connect(function()
   if LocalPlayer.Character ~= nil and LocalPlayer.Character.PrimaryPart ~= nil then
      local hrp = LocalPlayer.Character.PrimaryPart
      local dis = 0
      local targetposition
      for i, sellconsumable in pairs(workspace.sellConsumables:GetChildren()) do
         local distance = (hrp.Position - sellconsumable.Position).Magnitude
         if distance < dis then dis = distance targetposition = sellconsumable.Position end
      end
      hrp.CFrame = CFrame.new(targetposition) * CFrame.Angles(0,hrp.Orientation.Y,0)
   end
end)

What I’m doing here is looping through all the ‘SellConsumables’ in the workspace and if their distance is less than the distance holder im using, i will set the target pos to that consumable’s pos and the distance holder to the smaller distance. This way, we will know which sellconsumable is the closest. Magnitude basically converts the distance into a number from a Vector3 value

1 Like

Ahh, why dont you delete your mousebutton1click code and replace it with mine?. Also make sure there is a folder with all your sell areas in it called sellConsumables’. I will rewrite mine so gimme a mo

Edit: Updated my code

1 Like

hey thank you so much but idk how to fix this it says


and this is the script in the sellConsumable

local db = false

script.Parent.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)

	if player and player.Parent and not db then
		db = true

		local profile = playerData.Profiles[player]

		if profile and #profile.Data.Data.Consumables > 0 then
			profile.Data.Data.Slime = 100

			local soldItems = {} 
			local itemCounts = {}

			for i = #profile.Data.Data.Consumables, 1, -1 do
				local consumable = profile.Data.Data.Consumables[i]
				table.remove(profile.Data.Data.Consumables, i)

				if consumable == "plant" or consumable == "mushroom" or consumable == "flower" or consumable == "lotus" or consumable  == "Paper" or consumable == "Trash" then
					if consumable == "plant" then
						profile.Data.Data.Money = profile.Data.Data.Money + 5
					elseif consumable == "mushroom" then
						profile.Data.Data.Money = profile.Data.Data.Money + 10
					elseif consumable == "flower" then
						profile.Data.Data.Money = profile.Data.Data.Money + 200
					elseif consumable == "lotus" then
						profile.Data.Data.Money = profile.Data.Data.Money + 300
					elseif consumable == "Paper" then
						profile.Data.Data.Money = profile.Data.Data.Money + 600
					elseif consumable == "Trash" then
						profile.Data.Data.Money = profile.Data.Data.Money + 700
					end

					if not itemCounts[consumable] then
						itemCounts[consumable] = 0
					end
					itemCounts[consumable] = itemCounts[consumable] + 1
				end
			end

			for item, count in pairs(itemCounts) do
				table.insert(soldItems, {item = item, count = count})
			end

			if player:FindFirstChild("leaderstats") then
				player.leaderstats.Money.Value = profile.Data.Data.Money
				player.leaderstats["Slime Size"].Value = profile.Data.Data.Slime
			else
				print("Leaderstats not found for player: " .. player.Name)
			end

			game.ReplicatedStorage.events.frontEndSell:FireClient(player, soldItems)
		else
			print("Profile not found or no consumables for player: " .. player.Name)
		end

		task.wait(5) 
		db = false
	end
end)```

There is nothing wrong with the script, i am 100% sure the sell area is a model. Model’s don’t have a position property so you need to pick a basepart in the model to tp your character to. Lets say all your sell areas have a part ‘TpPart’ inside them (Please do that) Then just set targetposition to sellconsumable.TpPart.Position instead

1 Like