#"humanoid.Seated:Connect(function()" not firing

12 messages · Page 1 of 1 (latest)

vital nexus
#

When I run, the humanoid and VehicleSeat.Occupant both print my humanoid, but when I sit down on the ship, it doesnt seem to activate the connect event (the print statement isnt being printed). Is there something im missing with how roblox runs the code, or am I just using the wrong event.

For context, this is a client side script in the StarterPlayerScripts folder.

#
local UserInputService = game:GetService("UserInputService")
--Variables for current ship
local ship = nil
local controller = nil
local seat = nil
local cannons = nil
local cannonsLeft = {}
local cannonsRight = {}
--Config
local FireCannonsKey = Enum.KeyCode.F
local Key = nil
local Key = nil
local Key = nil

--Local player variables
local player = game.Players.LocalPlayer
local humanoid = nil

--Finds the character's humanoid when a character of the local player is added
player.CharacterAdded:Connect(function()
    humanoid = player.Character:FindFirstChildOfClass("Humanoid")
end)



while true do
    task.wait(5)
    print(ship)
    print(humanoid)
    print(workspace.Ship.ShipControls.VehicleSeat.Occupant)
end

--When the user sits in a seat, it gives the Captain tag if it doesnt already have it
humanoid.Seated:Connect(function()
    
    print("humanoid.Seated Event Fires")
    if player:HasTag("Captain") then 
        
    else
        ship = humanoid.SeatPart.Parent.Parent
        controller = humanoid.SeatPart.Parent
        seat = humanoid.SeatPart
        player:AddTag("Captain")
        cannons = controller.Parent.FindFirstChild("Cannons")
        for i in cannons do 
            if i:HasTag("left") then
                table.insert(cannonsLeft, i)
            elseif i:HasTag("right") then
                table.insert(cannonsRight, i)
            end
        end
    end
end)

red chasm
#

use spawn() instead

#
spawn(function

while true do
    task.wait(5)
    print(ship)
    print(humanoid)
    print(workspace.Ship.ShipControls.VehicleSeat.Occupant)
end


end)
#

explanation about spawn

basically it run a function on a thread, so it dont stop the rest of your code
(remember, when you run a code, it run in a linear way, but if you have a loop in it, it will stay on the loop until you break it)

so creating a thread, can avoid this rule. You can create "another script" inside your script (its a easily way to explain that)

red chasm
vital nexus
#

It doesn't work but at least I got a different error this time. The error was:
Players.Mustrdshaker.PlayerScripts.Keybinds:34: attempt to index nil with 'Seated'

Line 34 is "humanoid.Seated:Connect(function()" so I'm assuming it's trying to run it before the humanoid is set to anything? Which means I should add the connect under player.CharacterAdded to make sure humanoid is a valid type.

#

ima try that and see if it works

#

:0

#

it works ^-^