How to change the place a player will be teleported to depending on the item they have on their inventory

Hello guys, how are you doing? I’m struggling with this one problem in my game. Is there a way to teleport a player to a certain game depending on the item they have on their inventory?
So there’s this game I’m working on and the main goal is to escape your father’s house without him noticing it and head to the party. So one way to escape the house is by using the window, but I want to make it so that when the player uses the window without the item “Rope” in their inventory they will be teleported to a hospital, however if they have a rope in their inventory they will be teleported out of the house.

This is the code:

– Services –

local TeleportService = game:GetService(“TeleportService”)

local sound = workspace.WindowOpen

– Instances –

local ProximityPrompts = script[“Proximity Prompts”]

local Settings = script.Settings

– Variables –

local proximityPrompts = {}

local connections = {}

local placeId = Settings[“Place Id”].Value

local cityid = Settings[“City ID”].Value

– Functions –

local function getProximityPrompts()

for index, child in pairs(ProximityPrompts:GetChildren()) do

if child:IsA(“ObjectValue”) then

local prompt = child.Value

if prompt:IsA(“ProximityPrompt”) then

table.insert(proximityPrompts, prompt)

end

end

end

end

local replicatedstorage = game:GetService(“ReplicatedStorage”)

local teleportdoor = replicatedstorage:WaitForChild(“WindowEvent”)

local function onTriggered(player)

local chr = player.Backpack

if chr:FindFirstChild"Rope" then

sound:Play()

teleportdoor:FireClient(player, true)

TeleportService:Teleport(cityid, player)

elseif not chr:FindFirstChild"Rope" then

sound:Play()

teleportdoor:FireClient(player, true)

TeleportService:Teleport(placeId, player)

end

end

– Code –

getProximityPrompts()

for index, prompt in ipairs(proximityPrompts) do

local connection = prompt.Triggered:Connect(onTriggered)

table.insert(connections, connection)

end

Help

Any kind of help would be appreciated.

It seems you were pretty much there, I’ve just added a little bit which checks if the tool is being held by the player (and as such wouldn’t be in the player’s backpack).

local TeleportService = game:GetService("TeleportService")
local sound = workspace.WindowOpen
local ProximityPrompts = script["Proximity Prompts"]
local Settings = script.Settings
local proximityPrompts = {}
local connections = {}
local placeId = Settings["Place Id"].Value
local cityid = Settings["City ID"].Value

local function getProximityPrompts()
	for index, child in pairs(ProximityPrompts:GetChildren()) do
		if child:IsA("ObjectValue") then
			local prompt = child.Value
			if prompt:IsA("ProximityPrompt") then
				table.insert(proximityPrompts, prompt)
			end
		end
	end
end

local replicatedstorage = game:GetService("ReplicatedStorage")
local teleportdoor = replicatedstorage:WaitForChild("WindowEvent")

local function onTriggered(player)
	local char = player.Character
	local chr = player.Backpack
	if chr:FindFirstChild"Rope" or char:FindFirstChild"Rope" then
		sound:Play()
		teleportdoor:FireClient(player, true)
		TeleportService:Teleport(cityid, player)
	else
		sound:Play()
		teleportdoor:FireClient(player, true)
		TeleportService:Teleport(placeId, player)
	end
end

getProximityPrompts()
for index, prompt in ipairs(proximityPrompts) do
	local connection = prompt.Triggered:Connect(onTriggered)
	table.insert(connections, connection)
end
2 Likes

Thank you so much, I have been struggling with this issue for weeks.