How To Make More Then One Function Work

I am trying to make a script where if you touch a part UI come up. I have this for all of the parts but only the first function works. How do I make all of them work?

There are no errors in outputed, and the script is in StarterGui

Problom: When I hit the first one the UI come up but when I hit the other one UI should also come up But it does not. Like in the videio

local Animator = game.Workspace.Map.Jobs.HitBoxes:WaitForChild("Animator")
local Artist = game.Workspace.Map.Jobs.HitBoxes:WaitForChild("Artist")
local Builder = game.Workspace.Map.Jobs.HitBoxes:WaitForChild("Builder")
local ClothingDesigner = game.Workspace.Map.Jobs.HitBoxes:WaitForChild("ClothingDesiner")
local Composer = game.Workspace.Map.Jobs.HitBoxes:WaitForChild("Composer")
local Modeler = game.Workspace.Map.Jobs.HitBoxes:WaitForChild("Modeler")
local Scripter = game.Workspace.Map.Jobs.HitBoxes:WaitForChild("Scripter")
local UIDesigner= game.Workspace.Map.Jobs.HitBoxes:WaitForChild("UIDesigner")

Animator.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.Parent.JobUI.AnimatorFrame.Visible = true
		repeat task.wait() until (Animator.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 12
		script.Parent.Parent.JobUI.AnimatorFrame.Visible = false
	end
end)

Artist.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.Parent.JobUI.ArtistFrame.Visible = true
		repeat task.wait() until (Animator.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 12
		script.Parent.Parent.JobUI.ArtistFrame.Visible = false
	end
end)

Builder.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.Parent.JobUI.BuilderFrame.Visible = true
		repeat task.wait() until (Animator.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 12
		script.Parent.Parent.JobUI.BuilderFrame.Visible = false
	end
end)

ClothingDesigner.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.Parent.JobUI.ClothingFrame.Visible = true
		repeat task.wait() until (Animator.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 12
		script.Parent.Parent.JobUI.ClothingFrame.Visible = false
	end
end)

Composer.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.Parent.JobUI.ComposerFrame.Visible = true
		repeat task.wait() until (Animator.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 12
		script.Parent.Parent.JobUI.ComposerFrame.Visible = false
	end
end)

Modeler.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.Parent.JobUI.ModelerFrame.Visible = true
		repeat task.wait() until (Animator.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 12
		script.Parent.Parent.JobUI.ModelerFrame.Visible = false
	end
end)

Scripter.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.Parent.JobUI.ScripterFrame.Visible = true
		repeat task.wait() until (Animator.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 12
		script.Parent.Parent.JobUI.ScripterFrame.Visible = false
	end
end)

UIDesigner.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.Parent.JobUI.UIDFrame.Visible = true
		repeat task.wait() until (Animator.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 12
		script.Parent.Parent.JobUI.UIDFrame.Visible = false
	end
end)
4 Likes

What exactly is the issue though?

1 Like

When I hit the first one the UI come up but when I hit the other one UI should also come up But it does not. Like in the videio

2 Likes

I’m not 100% sure but I feel like it has something to do with that repeat task.wait() your doing, why dont you just use .TouchedEnded?

1 Like

How would I do that when I do it Animator.TouchEnded is underline in red

repeat Animator.TouchEnded until (Animator.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 12
1 Like

No no no , TouchEnded is it’s own event

part.Touched:Connect(function(hit)
   -- make ui visible
end)

part.TouchEnded:Connect(function(hit)
    -- make ui invisible
end)
1 Like

Put everything inside the events in a spawn function, for example:

UIDesigner.Touched:Connect(function(Hit)
spawn(function()
	if Hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.Parent.JobUI.UIDFrame.Visible = true
		repeat task.wait() until (Animator.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 12
		script.Parent.Parent.JobUI.UIDFrame.Visible = false
	end
>end)
end)
2 Likes

What I’d do is probably debug it, like if something is yielding my script, and print inside the touched event to see if it actually has been touched, otherwise it would be the gui issue, not the touched event.

1 Like

Wait but I thought the event are already on different threads?

1 Like

I also recommend using CollectionService, to make your touched events more organized and simpler.

1 Like

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