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

I need help trying to make something happen when a player sits down?

Asked by 2 years ago

Hi, I'm here once again to ask how I can make a ui pop up when a player sits down in a chair. But if I do that then nothing happens. It doesn't even print in the output.

    local gui = game.StarterGui.ScreenGui.Frame1 --the gui
    local object = script.Parent --the seat

    --Function Declaration
    local function onPartTouched(otherPart)
        -- Get the other part's parent
        local partParent = otherPart.Parent
        -- Look for a humanoid in the parent
        local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
        if humanoid then
        -- print I am running for confirmation that the script is at least functioning then move the ui
        print("Iamrunning")
            object.AnchorPoint = Vector2.new(0.5, 0.5)
            object.Position = UDim2.new(-0.5, -object.Size.X.Offset, 0.5, 0)
            object:TweenPosition(UDim2.new(0.07, 0.4, 0.45, .5))

            elseif nil then
            object.Position = Vector2.new(.25, .60)
        end
    end

    part.Touched:Connect(onPartTouched)--the function call

Can anyone say what I'm doing wrong. I don't think I quite understand how onPartTouched works.

0
I would use Humanoid.Seated instead of onPartTouched FBS_8 25 — 2y
0
@FBS_8 I'm not sure how changing it to humanoid.seated would fix it. onPartTouched is merely a function declaration. Unless there's something I'm missing here TheOof1sout 4 — 2y

2 answers

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

LocalScripts do not run in the Workspace, nor is a player's GUI located in StarterGui (the objects in StarterGui are cloned into the player's PlayerGui when they join the game). You will need to use RemoteEvents to communicate between a (server-side) script in the seat and a LocalScript that controls the GUI.

You use a lot of functions and other things that will work on a GUI but not a part/seat, and vice versa, and you haven't implemented anything for the GUI, but this is how you would do it.

Before changing the scripts, put a RemoteEvent in ReplicatedStorage, where both the server and the client can see it.

Seat script:

local seat = script.Parent

-- we need to save this because when someone gets out of the seat, "Occupant" will be nil and we won't be able to fire a RemoteEvent to them
local lastPlayer = nil

local function onOccupantChanged()
    if seat.Occupant then
        seat.Position = Vector3.new(0, 0, 0)

        local player = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)

    -- save the player in a variable so that we can do something when they leave
    lastPlayer = player

        game.ReplicatedStorage.SeatEvent:FireClient(player, true)
    else
        seat.Position = Vector3.new(0, 0, 0)
    game.ReplicatedStorage.SeatEvent:FireClient(lastPlayer, false)

    lastPlayer = nil
    end
end

seat:GetPropertyChangedSignal("Occupant"):Connect(onOccupantChanged)

GUI LocalScript:

local gui = script.Parent

local function onClientEvent(sitting)
    if sitting then
        -- sitting
    else
        -- not sitting
    end
end

game.ReplicatedStorage.SeatEvent.OnClientEvent:Connect(onClientEvent)

Alternatively, you could listen for when the "Sit" property changes on the humanoid in your GUI LocalScript without dealing with RemoteEvents in your seat script

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local function onSitChanged()
    if humanoid.Sit then
        -- show GUI
    else
        -- hide GUI
    end
end

humanoid:GetPropertyChangedSignal("Sit"):Connect(onSitChanged)
0
what would object be defined as? TheOof1sout 4 — 2y
0
oops, I made a typo. It's fixed now OfficerBrah 494 — 2y
0
alright, thanks man. Once again being a great help. TheOof1sout 4 — 2y
0
@OfficerBrah One last thing how do I make the gui disappear when they leave the seat TheOof1sout 4 — 2y
View all comments (2 more)
0
You can pass variables in RemoteEvents, so you could fire it with true if the person is sitting, or false if they got out of the seat. Or you could listen for if "Sit" changes in a LocalScript. I edited my post to show both OfficerBrah 494 — 2y
0
thanks man TheOof1sout 4 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

P.S. also I'd just like to put out there that I am using a local Script that is a child of the seat of I'm trying to program here

Answer this question