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:
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
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.
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?
--[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.
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…
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,
}