Why is this code not working?

The Code
local function touched(touchingPart)
	if touchingPart.Parent:FindFirstChild("Humanoid") then
		print("ok")
		local player = touchingPart.Parent
		local cam = workspace.CurrentCamera
		cam.CameraType = Enum.CameraType.Scriptable
		local CF = game.Workspace.MarketCam.CFrame
		cam.CFrame = CF
	end	
end


game.Workspace.MarCam.Touched:Connect(touched)

I have this code. But it’s not working I am not getting any errors. It’s supposed to change the players cameras CFrame

1 Like

You are not passing the argument “touchingPart” in the function.

the print(“ok”) works. (30 cahr)

The Touched function passes in that argument automatically.

Perhaps the camera or the character has not loaded.

I assume that this is a server script, right? Camera stuff has to be done in a LocalScript. Here is some info.

https://developer.roblox.com/en-us/articles/Roblox-Client-Server-Model

Also, make sure that the Touched function works in a LocalScript. I haven’t tried it, but I assume it works.

1 Like

This needs to be ran on the client, since you’re changing their camera ( really hope you are );

Instead of making the player = to the hit.Parent ( super incorrect ) do this;

--//Services

local Players = game:GetService("Players")

--//Variables

local LocalPlayer = Players.LocalPlayer

--//Events

-- your event and code

local Player =  Players:GetPlayerFromCharacter(touchingPart.Parent)
if Player == LocalPlayer then --makes sure you can actually change their camera and it was your local player on the client that hit the part

Anyways best of luck, hope this worked. If this was a server script, I’d highly recommend making it a local script and checking if the local player was the one who hit it, as the camera can only be changed locally for a player.

1 Like

Thank you for your help. Why is making

this incorrect?

Yes, highly incorrect. When the player touches the part, it is not their player touching the part, it is their character that connected this event ( one of the parts in their character ). This is why you’re checking for a Humanoid, and doing part.Parent, because it was the character that did the event if a Humanoid exists. Which is why Roblox created GetPlayerFromCharacter to make it easier for you to get a player from their character ( instead of hypothetically having to do the event you did and or doing players:FindFirstChild(character.Name) ).

1 Like