Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

(Semi-Complicated) How to define a changing variable for OnTouch?

So I have these two local scripts that manage package drop-offs for my delivery sim, problem is the location is randomized doors with street names and idk how to define which door aka (street.name) the touch function needs to be watching for, trying to avoid remoteevents if possible

Address Randomizer

while wait(60) do
	local Folder = game.Workspace.Folder:GetChildren()
	local Street = Folder[math.random(1, #Folder)]
	plr.PlayerGui.Address.Text = "New Order: Deliver to "..Street.Name
end

OnTouch Front Door

local part = ????
local function onTouched(part)
	--Do stuff when player touches 'Street' from script above
end
part.Touched:Connect(onTouched)

1 answer

✓ Accepted answer

I would name all of the doors the same thing such as "DelieveryDoor" or something, and then put a string inside of the door with the info you need. Then you could do this:

while wait(60) do
    local Folder = game.Workspace.Folder:GetChildren()
    local Street = Folder[math.random(1, #Folder)]
    plr.PlayerGui.Address.Text = "New Order: Deliver to "..Street.Name
end

for i, v in pairs(game.Workspace:GetChildren()) do
	if v.Name == "DelieveryDoor" then
		local function onTouched(v)
			if v.StringValue.Value == Street.Name then
				--Delievered!
			end
		end
		v.Touched:Connect(onTouched)
	end
end

1 comment

1

Very good answer thank you, unfortunately the delivery address comes from the door name, if I name the doors all the same then the GUI text will just put out the same address for all houses, but I may be able to get around this by putting each door into a folder, not sure, thx tho

This discussion is preserved as it was. New answers, comments, and votes are closed.