Why Touched:Connect(function() isn't working?

So I’m trying to understand why this function is not being triggered from a long time but still idk.

local Model = script.Parent
local Config = Model.Configuration
local userId = Config.userId.Value
local FriendBot = script.Parent.Parent.Name
local button = script.Parent.Parent.Parent:FindFirstChild(FriendBot .. "Button") ---------------------- nameee




local Loader

if Config.AutoUpdateLoader.Value then
	Loader = require(552034302) 
else
	Loader = require(script.MainModule)
end

-------------------------------------------------------------------------------------

Loader:updateModel(Model, Config.userId.Value)


button.Head.Touched:Connect(function()
	print("Touched")
	while userId == 2383848373 do
		task.wait(0.1)
	end 
	Loader:updateModel(Model, Config.userId.Value)
	print("AVATAR LOADED")
end)

The script need to load the avatar of a user from his id to a dummy using the Loader:updateModel(Model, Config.userId.Value) function. That’s working but I want to trigger it when the button is touched so that’s what I’ve made. Can anyone tell me where I’m wrong and why the button.Head.Touched:Connect(function() function is not being called. I tested it and the button is being recognized and have a children called “Head”. Thanks for the help, probably that’s simple one but I’m new to scripting and that’s not happening for the first time to me.

2 Likes

First of all, what type of script is this? Is it a local script or a server script?

oh I forgot to tell that, It’s server script

So the i think the reason why it would never en is becuase your not checking if it has a humanoid and as soon as anyparts touches it the loop woill began but since there is a userid varible is set to the number you have to set in a task.spawn(function() and have a if statement checking for a humanoid

Script

local Model = script.Parent
local Config = Model.Configuration
local userId = Config.userId.Value
local FriendBot = script.Parent.Parent.Name
local button = script.Parent.Parent.Parent:FindFirstChild(FriendBot .. "Button") ---------------------- nameee




local Loader

if Config.AutoUpdateLoader.Value then
	Loader = require(552034302) 
else
	Loader = require(script.MainModule)
end

-------------------------------------------------------------------------------------

Loader:updateModel(Model, Config.userId.Value)


button.Head.Touched:Connect(function(hit)
If hit.Parent:FindFirstChild("Humanoid")
	print("Touched")
task.spawn(function()
	while userId == 2383848373 do
		task.wait(0.1)
	end 
end)
	Loader:updateModel(Model, Config.userId.Value)
	print("AVATAR LOADED")
end
end)

make sure that “CanTouch” Property on the Button is set to true

also, is the print(“Touched”) running? (If it is, it’ll likely print “Touched” multiple times since it’s not checking if a player triggered it)

Hi There!

Ii don’t know if it will help, but in my experience i struggle a bit with OnTouch events. They only activate if the part moves and touches the other, which means that anchored parts or parts created that are already touching the part won’t trigger the event

You said there that the button have a children called “Head”. You saw that trought the print function or you’re saying that you added a children called “Head”?

If it was from printing, the Head is the part of the character that is touching the button

Hope it helps :wink:

This here would work. Something else is wrong.

The “Head” is already there:
image
Also the script inside the head have a function that works:

function onTouched(hit)
	if ting == 0 then
	ting = 1
	
	local sound = Instance.new("Sound")
	sound.SoundId = "rbxassetid://5613553529"
	local owner = factory.OwnerName.Value
	local check = hit.Parent:FindFirstChild("Humanoid")
	local player = game.Players:FindFirstChild(owner)
	if check ~= nil then
		local user = game.Players:GetPlayerFromCharacter(hit.Parent) 
		if owner == user.Name then
			local stats = user:findFirstChild("leaderstats")
			if stats ~= nil then
					local cost = 5 ------------------------------------------------------------------------------ PRIZE
					if player.leaderstats.Money.Value >= cost then 
						script.Parent.Parent:SetPrimaryPartCFrame(newPivot)
						Friend2Button:SetPrimaryPartCFrame(rightPivot) -------------------------------pos
						Friend3Button:SetPrimaryPartCFrame(rightPivot2)
					player.leaderstats.Money.Value  -= cost 
					local machine = factory.Stuff.Friend1.Value:Clone() --------------------------------------------------------------------- NAME
					machine.Parent = factory
					machine:MakeJoints()
					local button = script.Parent.Parent
					button.Head.Transparency = 1
					button.Head.Glow:Destroy()
					button.Head.Union:Destroy()
					button.Head.BillboardGui:Destroy()
					button.Head.BillboardGui2:Destroy()
					local Friend = game.Workspace["Abonded Tycoon"].All.Tycoon.Friend1 ---------------------------------------------------------------- NAME
					sound.Parent = game.Workspace["Abonded Tycoon"].All.Tycoon:WaitForChild(Friend.Name)["Study Desk"].SoundPart
					sound.RollOffMaxDistance = 30
					while true do
						local player = game.Players:FindFirstChild(owner)
						local loadingBar = Friend.Rig.PartUI.BillboardGui.CanvasGroup.Progress
						local Timer = Friend.Rig.PartUI.BillboardGui2
						local n = math.round(timerVal) 
						local timersize = n
						local barsize = loadingBar.Size.X.Scale
						barsize = 0.001
						loadingBar.Size = UDim2.new(barsize, loadingBar.Size.X.Offset, loadingBar.Size.Y.Scale, loadingBar.Size.Y.Offset)
						Timer.Text.Text = timersize .."s"
							for i = 1, 100, 1 do
								Timer.Text.Text = math.round(timersize * 10)/10 .. "s"
								barsize += 0.01
								loadingBar.Size = UDim2.new(barsize, loadingBar.Size.X.Offset, loadingBar.Size.Y.Scale, loadingBar.Size.Y.Offset)
								timersize -= n/100
								task.wait(n/100)
							end
						Timer.Text.Text = "0s"
						local BanEvent = game:GetService('ReplicatedStorage'):WaitForChild('BanEvent')
						BanEvent:FireClient(player)
						sound:Play()
						task.wait(0.619)
						sound:Stop()
					end
				end
			end
		end

	end
	ting = 0
	end
end

script.Parent.Touched:connect(onTouched)

And I don’t understand why this script works but the second one that I made the post for doesn’t.

I thought so too, but I checked everything I could.

The problem is that the print(“Touched”) isn’t running and yes the “CanTouch” is on. Also like I said

I tried your code but still it doesn’t work. Also I don’t think the humanoid is a problem since the head part inside the button is anchored. That means only non anchored objects can trigger the event and even them can’t trigger it since the print line is not running.

Maybe thats to do with the Head being a Union Part

Try checking the collision fidelity (I think thats the name) and make the other parts of the model with the Cancollide false

But the function is being triggered from another script so I don’t think this should be a problem.

Another thing I noticed, the button variable is getting a part called “FriendBotButton”, not the “Friend1Button”

But maybe thats right and im just yapping here

I made it like this because there are more then one friend and I will make even more so to win some time I made it like that. Also the part is not getting called “FriendBotButton” because FriendBot is equal to FriendX where X is a number.

1 Like

I finally made it! So I didn’t solve the problem with the Touched:Connect(function() I just remembered that this script is being triggered when the player touch the button so here is the new script:

local Model = script.Parent
local Config = Model.Configuration
local userId = Config.userId.Value
local FriendBot = script.Parent.Parent.Name
local button = script.Parent.Parent.Parent:FindFirstChild(FriendBot .. "Button") 




local Loader

if Config.AutoUpdateLoader.Value then
	Loader = require(552034302) 
else
	Loader = require(script.MainModule)
end

-------------------------------------------------------------------------------------

Loader:updateModel(Model, Config.userId.Value)

task.spawn(function()
	while userId == 2383848373 do
		task.wait(0.1)
	end 
end)

task.wait(1)

Loader:updateModel(Model, Config.userId.Value)
print("DONE")

I’m using the function that @MrNobodyDev said to use:

Thanks to everyone who helped, I really appreciate it!

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