Touched is not Working on Script

Hello there!

I created a script at the ServerScriptService, and placed on a folder called “Camera” with the function to change the CFrame of the player when he touches a part that has the tag called “Zone”:

--[Services]--
local ServerStorage = game:GetService("ServerStorage")

local CollectionService = game:GetService("CollectionService")

--[Zone Camera]--
local zoneCamera = ServerStorage:WaitForChild("ZoneCamera").Camera

--[Debounce Variable]--
local debounce = {}

--[Local Function]--
local function changeCamera(player)
	
	if debounce[player] then
		
		return -- Exit the function if the player is still on debounce
		
	end

	debounce[player] = true -- Set debounce to true for the player

	local camera = game.Workspace.CurrentCamera
	
	camera.CameraType = Enum.CameraType.Scriptable
	
	camera.CFrame = zoneCamera.CFrame

	task.wait(1) -- Adjust the wait time to your desired debounce duration

	debounce[player] = false -- Reset debounce to false for the player
end

local function resetCamera(player)
	local camera = game.Workspace.CurrentCamera
	
	camera.CameraType = Enum.CameraType.Custom -- Reset the camera type to default
	
	-- Set the camera position and rotation to the player's character
	camera.CFrame = player.Character.HumanoidRootPart.CFrame
end

--[Calling Event]--
for _, part in ipairs(CollectionService:GetTagged("Zone")) do
	
	part.Touched:Connect(function(otherPart)
		
		local character = otherPart.Parent
		
		local player = game.Players:GetPlayerFromCharacter(character)
		
		if player then
			
			changeCamera(player)
			
		end
		
	end)

	part.TouchEnded:Connect(function(otherPart)
		
		local character = otherPart.Parent
		
		local player = game.Players:GetPlayerFromCharacter(character)
		
		if player then
			
			resetCamera(player)
			
		end
		
	end)
	
end

With the script and playtesting on Roblox Studio, it always prints on the Output window that the event .Touched is not valid:

image


Does anyone know how I can solve this problem?

Thank you!

2 Likes

make sure that in the for _, part you add a if part:IsA(“BasePart”) so that it checks that it is a surface, because if there IS a script, then it will break the loop

1 Like

Furthermore, I also tried to see what was the ClassName of the part and at first, it printed “Part”, then “Script” after one millisecond:

image

1 Like

You accidentally added a Script to your Zones tag then.

Also, you don’t need to set the position back to reset the camera. Roblox automatically does it for you.

Code:

local function resetCamera()
	local camera = workspace.CurrentCamera
	camera.CameraType = Enum.CameraType.Custom -- Reset the camera type to default
end
2 Likes

I am not sure, I read a bit about how to use the CollectionService in a nutshell way, and when they created a code to execute utilizing the defined tag, it worked:

I did not add a script to the zone tags, I believe it would be impossible as everything is separated from each other and this was not the expected result I wanted to achieve…


I didn’t know about this information, I will change it after trying to comprehend why this code keeps breaking LOL.

1 Like

Okay, I have changed this part, but I am still thinking how was I able to create a script for the tag “Zone”… How would I be able to remove it from it?

1 Like

Still looking for help on how I can fix this…

Did this and it provided a print message saying that the ClassName is a Script instead of a Part…

image

I am not sure how I can “rollback” and get the part by the tag I defined…

1 Like

Can I see how you did it, that didn’t happen to me

1 Like

Sure! Here is how I did it:

--[Calling Event]--
for _, part in ipairs(CollectionService:GetTagged("Zone")) do
	
	if part:IsA("BasePart") then
		
		print("It is a " .. part.ClassName)
		
	end
	
	print("It is a " .. part.ClassName)
	
	part.Touched:Connect(function(otherPart)
		
		local character = otherPart.Parent
		
		local player = game.Players:GetPlayerFromCharacter(character)
		
		if player then
			
			changeCamera(player)
			
		end
		
	end)

	part.TouchEnded:Connect(function(otherPart)
		
		local character = otherPart.Parent
		
		local player = game.Players:GetPlayerFromCharacter(character)
		
		if player then
			
			resetCamera(player)
			
		end
		
	end)
	
end

Use this script, and look at what instances it prints. If the instance is a script, look for it in your explorer and get rid of the tag attached.

Code:

--[Calling Event]--
for _, part in CollectionService:GetTagged("Zone") do
	print(part:GetFullName())

	if part:IsA("BasePart") then

		print("It is a " .. part.ClassName)

	end

	print("It is a " .. part.ClassName)

	part.Touched:Connect(function(otherPart)

		local character = otherPart.Parent

		local player = game.Players:GetPlayerFromCharacter(character)

		if player then

			changeCamera(player)

		end

	end)

	part.TouchEnded:Connect(function(otherPart)

		local character = otherPart.Parent

		local player = game.Players:GetPlayerFromCharacter(character)

		if player then

			resetCamera(player)

		end

	end)

end

I also noticed a problem with your script. You can’t use server scripts to modify player’s current cameras, you will have to use RemoteEvents to communicate with the client.

image

Pretty much a crazy situation I am experiencing so far (LOL)…

Wait, what? Does that mean the local functions the script has will need to be deleted and rewritten by invoking RemoteEvents? :anguished:

Yes.

So remove the tag from ServerScriptService.Camera.Script then.

Yikes ! Not going to lie, I am pretty terrible at comprehending RemoteEvents (plus RemoteFunctions) and how I can invoke and communicate to Server/Client/Both sides… :skull:

Okay, I have returned and I tried improving my script (from ServerScriptService) while trying to use the RemoteEvent, which you can find at the end of the code below. I am not sure if this is correct, but I tried… (Keep in mind that I am absolutely terrible at understanding RemoteEvents as well as RemoteFunctions Touched is not Working on Script - #13 by JuanGamerPlayz_RBLX)

--[Services]--
local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")

--[RemoteEvent]--
local remoteEvent = ReplicatedStorage.RemoteEvents.ChangeCamera.RemoteEvent

--[Zone Camera]--
local zoneCamera = ServerStorage:WaitForChild("ZoneCamera").Camera

--[Zone Part]--
local zonePart = game.Workspace:WaitForChild("Zone").FruitZone.Zone

--[Debounce Variable]--
local debounce = {}

--[Local Functions]--
local function changeCamera(player)
	
	if debounce[player] then
		
		return -- Exit the function if the player is still on debounce
		
	end

	debounce[player] = true -- Set debounce to true for the player

	local camera = zoneCamera
	
	camera.CameraType = Enum.CameraType.Scriptable
	
	camera.CFrame = zoneCamera.CFrame

	task.wait(1) -- Adjust the wait time to your desired debounce duration

	debounce[player] = false -- Reset debounce to false for the player
	
end

local function resetCamera(player)
	
	local camera = game.Workspace.CurrentCamera
	
	camera.CameraType = Enum.CameraType.Custom

	-- Set the camera position and rotation to the player's character
	
	local character = player.Character
	
	if character then
		
		camera.CFrame = character:WaitForChild("HumanoidRootPart").CFrame
		
	end
	
end

local function onPartTouch(otherPart)
	
	local player = Players:GetPlayerFromCharacter(otherPart.Parent)
	
	if player then
		
		changeCamera(player)
		
	end
	
end

local function onPartTouchEnded(otherPart)
	
	local player = Players:GetPlayerFromCharacter(otherPart.Parent)
	
	if player then
		
		resetCamera(player)
		
	end
	
end

zonePart.Touched:Connect(onPartTouch)

zonePart.TouchEnded:Connect(onPartTouchEnded)

--[Event Connections]--
remoteEvent.OnServerEvent:Connect(onPartTouch, onPartTouchEnded)

return {
	
	ChangeCamera = changeCamera,
	ResetCamera = resetCamera,
	
}

I’m still looking for help on how I can solve this little rocket science issue! :slightly_smiling_face: