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

Unable to cast value to Object?

Asked by 4 years ago

Some background, I am deving a game and I want the camera to change when you open the door. I wanted to make it easier so I didn't have to use 25000 events, but I need help since I keep on getting the error above. Script:

local TweenService = game:GetService("TweenService")
local CamE = game.ReplicatedStorage:WaitForChild("CameraEvent")
local Cam1 = false
local Cam2 = false
local Cam3 = false
local door1 = game.Workspace:WaitForChild("Door1")
local door2 = game.Workspace:WaitForChild("Door2")
local tweeningInformation = TweenInfo.new(
    0.4,    -- Speed
    Enum.EasingStyle.Linear,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)
local door1Open = {CFrame = CFrame.new(91.66, 4.05, -17.51) * CFrame.Angles(math.rad(0), 90, 0)}
local door2Open = {CFrame = CFrame.new(82.17, 4.05, -17.51) * CFrame.Angles(math.rad(0), -90, 0)}
local door1Close = {CFrame = CFrame.new(89.07, 4.05, -19.17)}
local door2Close = {CFrame = CFrame.new(84.27, 4.05, -19.17)}
local tween1open = TweenService:Create(door1,tweeningInformation,door1Open)
local tween1close = TweenService:Create(door1,tweeningInformation,door1Close)
local tween2open = TweenService:Create(door2,tweeningInformation,door2Open)
local tween2close = TweenService:Create(door2,tweeningInformation,door2Close)

game.Workspace.Detector1.Touched:Connect(function(hit)
    Cam1 = true
    CamE:FireClient(Cam1) -- ERROR LINE
    door1.CanCollide = false
    door2.CanCollide = false
    tween1open:Play()
    tween2open:Play()
    wait(1)
    tween1close:Play()
    tween2close:Play()
    door1.CanCollide = true
    door2.CanCollide = true
end)
-- script.Parent.Detector2.Touched:Connect(function(hit)
    --tween1open:Play()
    --tween2open:Play()
    --wait(2)
    --tween1close:Play()
    --tween2close:Play()
--end)

Local Script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local CameraE = game.ReplicatedStorage:WaitForChild("CameraEvent")
print("e") -- used to test to see if working

CameraE.OnClientEvent:Connect(function(Cam1)
    if Cam1 == true then
repeat wait()
    Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable
        Camera.CFrame = workspace.Camera1.CFrame
    --else if Cameratype[2] == true then

        --end
    end
    end)


0
which line is the error occurring on, and in what script? sergeant_ranger 184 — 4y
0
If you view the script to the full extent its line 27 MrRobWasHere 22 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

It is because the first argument of :FireClient() needs to be the player you're firing to. So to get it, we can use the hit argument of .Touched:

part.Touched:Connect(function(hit)

    if not hit then return end
    if not hit.Parent then return end
    if not hit.Parent:FindFirstChild("Humanoid") then return end

    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- we are gonna get the player with their character (same as game.Players[hit.Parent.Name])

end)

So let's implement that into your script:

local TweenService = game:GetService("TweenService")
local CamE = game.ReplicatedStorage:WaitForChild("CameraEvent")
local Cam1 = false
local Cam2 = false
local Cam3 = false
local door1 = game.Workspace:WaitForChild("Door1")
local door2 = game.Workspace:WaitForChild("Door2")
local tweeningInformation = TweenInfo.new(
    0.4,    -- Speed
    Enum.EasingStyle.Linear,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)
local door1Open = {CFrame = CFrame.new(91.66, 4.05, -17.51) * CFrame.Angles(math.rad(0), 90, 0)}
local door2Open = {CFrame = CFrame.new(82.17, 4.05, -17.51) * CFrame.Angles(math.rad(0), -90, 0)}
local door1Close = {CFrame = CFrame.new(89.07, 4.05, -19.17)}
local door2Close = {CFrame = CFrame.new(84.27, 4.05, -19.17)}
local tween1open = TweenService:Create(door1,tweeningInformation,door1Open)
local tween1close = TweenService:Create(door1,tweeningInformation,door1Close)
local tween2open = TweenService:Create(door2,tweeningInformation,door2Open)
local tween2close = TweenService:Create(door2,tweeningInformation,door2Close)

game.Workspace.Detector1.Touched:Connect(function(hit)

    if not hit then return end
    if not hit.Parent then return end
    if not hit.Parent:FindFirstChild("Humanoid") then return end

    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

    Cam1 = true
    CamE:FireClient(player,Cam1) 

    door1.CanCollide = false
    door2.CanCollide = false

    tween1open:Play()
    tween2open:Play()

    wait(1)

    tween1close:Play()
    tween2close:Play()

    door1.CanCollide = true
    door2.CanCollide = true

end)
-- script.Parent.Detector2.Touched:Connect(function(hit)
--tween1open:Play()
--tween2open:Play()
--wait(2)
--tween1close:Play()
--tween2close:Play()
--end)

And the local script is fine, except you don't need to do

repeat 

    wait()
    Camera.CameraType = Enum.CameraType.Scriptable

until Camera.CameraType == Enum.CameraType.Scriptable

as that was a problem some years ago and I believe it is normal now so do Camera.CameraType == Enum.CameraType.Scriptable.
Hope this helped!

Ad

Answer this question