Is there any way to reference an object in a script without knowing its exact location?


local RS = game:GetService("ReplicatedStorage")
local Event = RS.HotPotatoSelected
local Start = RS.PotatoCreateEvent

local Potato = Instance.new("Accessory")
		RS.Handle.Parent = Potato
		local Attatchment = Instance.new("Attachment")
		Attatchment.Name = "BodyFrontAttachment"
		Attatchment.Parent = Potato.Handle
Potato.Parent = RS
Potato.Name = "Potato"

Attatchment.Orientation = Vector3.new(0,0,-90)
	print("Potato Instance has been created")

Event.OnServerEvent:Connect(function()
	local Players = game.Players:GetPlayers()
	local random = Players[math.random(1,#Players)]

	Start.OnServerEvent:Connect(function()
		local PotatoClone = Potato:Clone()

		PotatoClone.Parent = random.Character
		print("Potato Instance has been attached to the player")

		local cooldown = true
		
			PotatoClone.Handle.Touched:Connect(function(hit)
			if hit.Name == "HumanoidRootPart" then
				if cooldown then
					cooldown = false
					PotatoClone.Parent = hit.Parent
					print("Swapped Potato to",hit.Parent)
					wait(2)
					warn("cooldown done")
					cooldown = true
				end

			end
			
			end)
	end)
end)

This script is for a hot potato game. It will make a potato then put it on a random player, then whoever is the potato can pass it on to another player by touching them.
I’m trying to make it so that in another script, a while loop runs. This loop will count down the time until the potato blows up. Is there a way to reference the potato in that script without knowing where exactly it is?

have you tried parenting the second script to the potato? then you could just do script.Parent

I’ll try that. I feel kinda dumb now not realizing that earlier

Why not have both things happen in the same script?

If you are referring to an objects directory / full name (which I believe is the case), you could use an ObjectValue.

Set the ObjectValue’s value to your instance, and you will be able to refer to the instance via the ObjectValue‘s value.


1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.