Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I change CameraType on TouchEnded?

Asked by 6 years ago

This script is supposed to change the Player's CameraType when the Player stops touching a brick.

local Cam = game.Workspace.CurrentCamera
script.Parent.TouchedEnded:connect(function(hit)
    wait(1)
    if Cam.CameraType == "Scriptable" and game.Players:GetPlayerFromCharacter(hit.Parent) then
        Cam.CameraType = "Custom"
        else
    end
end)

Yet, it does not work and doesn't even show an error(yes, this code is in a Local Script).

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Errors in your code:

I. You tried to use the Touched/TouchEnded events on the client, use a RemoteEvent and fire the Camera change to the client, while handling the touch event from the server.

II. You are using deprecated code.RBXScriptSignal:connect() is deprecated, switch to RBXScriptSignal:Connect()

III. In line 4, CameraType holds an Enum value, although technically it’s okay to assign an Enum value with a string, if you are checking if an Enum property value equals a specific enum, you must specify the Enum in conditional statements. These are conditional statements, and if a property holds an Enum value, the Enum value must be specified. Take these for example:

if Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
    --code
end

while PlayerGui.ScreenGui.Frame.Style == Enum.FrameStyle.ChatGreen do
    -- code
end

repeat
    -- code
until game.Workspace.Part.Shape == Enum.PartType.Ball

IV. The event is BasePart.TouchEnded, not TouchedEnded.

--Server

script.Parent.TouchEnded:Connect(function(part)
    local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)

    if plr then
        game.Workspace.ChangeCamType:FireClient(plr)
    end
end)

Code for the remote event.

--Local Script

game.Workspace.ChangeCamType.OnClientEvent:Connect(function()
    local camera = game.Workspace.CurrentCamera

    if camera.CameraType == Enum.CameraType.Scriptable then -- Here was your error. Must check with an enum in conditional statements.

        camera.CameraType = Enum.CameraType.Custom --It’s always good to use enums instead of strings to assign enum values, since they are enums. 
    end
end)
0
No it isn't mandatory. I could do repeat wait() workspace.Part.Shape = "Ball"; until workspace.Part.Shape == "Ball"; and it would still work. Zafirua 1348 — 6y
0
no... User#19524 175 — 6y
0
You need to use an enum in conditional statements. http://wiki.roblox.com/index.php/#Using_Enumerations User#19524 175 — 6y
0
What would 'ChangeCamType' be? Meerkatclaw06 6 — 6y
0
A RemoteEvent inside of Workspace. User#19524 175 — 6y
Ad

Answer this question