Search code examples
if-statementeventsluarobloxtween

Lua Tween Part Info


I am making a tween that uses data given from a Humanoid.Seated event, and I wanted to make the camera go to the end point when sat down, however, move back after they sat up. I have a feeling that the problem is with the part info, however I could be wrong.

This is the code:

The Sender/Event Handler:

local camPart = script.Parent
local camEvent = game.ReplicatedStorage.CamEvent
local blueSeat = script.Parent.Parent.BlueSeat.Seat --the correct seat person should be in

local bluePlayerName = script.Parent.Parent.Buttons.BlueEnter.PlayerName --the supposed name of person

bluePlayerName:GetPropertyChangedSignal("Value"):Connect(function ()
    if (bluePlayerName ~= "") then
        
        local char = game.Workspace:FindFirstChild(bluePlayerName.Value, true)
        local player = game.Players:GetPlayerFromCharacter(char)
        
        char.Humanoid.Seated:Connect(function (isSeated, seat)
            
            if (seat.Name == blueSeat.Name) then
            
                camEvent:FireClient(player, camPart, isSeated) --go to tween handler
            end
        end)
    end
end)

The Receiver/Tween Handler:

local TweenService = game:GetService("TweenService")
local cam = game.Workspace.Camera
local partData
local tween
local length = 2

local tweenData = TweenInfo.new(
    length,
    Enum.EasingStyle.Sine,
    Enum.EasingDirection.Out,
    0,
    true,
    0
)

script.Parent.OnClientEvent:Connect(function (camPart, isSeated) --receiver
    
    partData = {
        CFrame = camPart.CFrame
    }
    
    tween = TweenService:Create(cam, tweenData, partData)
    
    if (isSeated == true) then
    
        cam.CameraType = Enum.CameraType.Scriptable --remove control
        tween:Play()
        
        wait(length / 2)
        tween:Pause() --stop at end point
        
    elseif (isSeated == false) then

        tween:Play() --go back/finish
        wait(length / 2)
        
        cam.CameraType = Enum.CameraType.Custom --give control back
    end
end)

Solution

  • The fact that the RemoteEvent isn't firing at all should be an clue that the connection to the Humanoid.Seated event isn't being reached in the server Script. It's unclear from your code sample what would trigger the code in the first place, but it looks like you're just looking for when a player's character loads into the workspace.

    I would recommend using the Player.CharacterAdded or Player.CharacterAppearanceLoaded events as ways of getting access to the player's Character and humanoid. You can still use your UI code as a trigger for whether to tween or not, but it might be easier.

    -- Server Script
    local camPart = script.Parent
    local camEvent = game.ReplicatedStorage.CamEvent
    local thing = script.Parent.Parent
    local blueSeat = thing.BlueSeat.Seat --the correct seat person should be in
    local bluePlayerName = thing.Buttons.BlueEnter.PlayerName --the supposed name of person
    
    -- listen for when a player sits in a seat
    game.Players.PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function(character)
            character.Humanoid.Seated:Connect(function(isSeated, seat)
                print("Player is seated?", isSeated)
                if not isSeated then
                   -- tell the client to zoom out
                   camEvent:FireClient(player, camPart, isSeated)
                else
    
                    -- decide whether to tween the camera
                    local isApprovedSeat = seat.Name == blueSeat.Name
                    local isNameSet = bluePlayerName.Value ~= ""
                    local shouldTweenCamera = isApprovedSeat and isNameSet
                    if shouldTweenCamera then
                        camEvent:FireClient(player, camPart, isSeated)
                    else
                        local message = table.concat({
                            "Camera not tweening because: ",
                            "Player has claimed this seat? " .. tostring(hasClaimedSeat),
                            "This is the approved seat? " .. tostring(isApprovedSeat)
                        }, "\n")
                        warn(messsage)
                    end
                end
            end)
        end)
    end)
    

    Also, it looks like the LocalScript that is listening for this RemoteEvent is located in ReplicatedStorage. Check the documentation on LocalScripts, they only fire in a handful of locations, and ReplicatedStorage unfortunately isn't one of them. Try moving the LocalScript into StarterCharacterScripts and update the path to the RemoteEvent.

    local camEvent = game.ReplicatedStorage.CamEvent
    camEvent.OnClientEvent:Connect(function (camPart, isSeated) --receiver