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

How do I make it so when someone sits in a VehicleSeat? (question continued)

Asked by 10 years ago

(continued from title) it gives them a GUI, then when they get out, it removes that GUI?

I know that the title shouldn't have a question mark, but I'm supposed to have one in there.

Here's what I got (all I have is the GUI giver):

--ScrewGuests
local sp = script.Parent;

function T(P)
    sp.Parent.Parent = P.Parent
    local plr = game:GetService('Players'):FindFirstChild(sp.Parent.Name)
    local hud = plr.PlayerGui:FindFirstChild("MphKphFlipGui")
    if hud == nil then
        script.MphKphFlipGui:Clone().Parent = plr.PlayerGui
    end
end

sp.Touched:connect(T)

The setup of the VehicleSeat is: VehicleSeat (VehicleSeat) -> GiveGui (Script) -> MphKphFlipGui (ScreenGui)

2 answers

Log in to vote
0
Answered by 10 years ago

** - Put the Script inside a Seat. - Remove the GUI already inside the Script (It's just an example). - Put your own GUI inside the script. - Re-name the GUI to "GUI" (Without the quotation marks " ")**

Players = Game:GetService("Players")
Workspace = Game:GetService("Workspace")

Seat = script.Parent
GUI = script.GUI
Active = false
Player = nil
NewGui = nil

Seat.ChildAdded:connect(function(Child)
    if Active == false then
        if Child.Name == "SeatWeld" then
            Active = true
            Player = Players:GetPlayerFromCharacter(Child.Part1.Parent)
            if Player ~= nil then
                PlayerGui = Player:FindFirstChild("PlayerGui")
                if PlayerGui ~= nil then
                    NewGui = GUI:Clone()
                    NewGui.Parent = PlayerGui
                end
            end
        end
    end
end)

Seat.ChildRemoved:connect(function(child)
    if child.Name == "SeatWeld" then
        if Active == true then
            Active = false
            pcall(function() NewGui:Destroy() end)
            Player = nil
            NewGui = nil
        end
    end
end)

For the model go:

0
Sorry about the link not being there.. Roblox isn't allowing me too set the script as for sale. PGenocide 5 — 10y
0
It's okay. Ugh_Emily 25 — 10y
0
If it doesn't work PM me on roblox my Username is: PGenocide PGenocide 5 — 10y
Ad
Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
10 years ago

The easiest way I find to do this is to use the ChildAdded and ChildRemoved events. When someone sits in a seat it creates a seat weld between the character and seat, so you can very simply check when someone sits in a seat and who it was as well.

local driver -- This will hold a value for who sat in the seat
local seat = script.Parent -- Location of the seat
local gui = game.ServerStorage.ScreenGui -- Location of gui

seat.ChildAdded:connect(function(added)
    if added.Name == "SeatWeld" then
        driver = seat.SeatWeld.Part1.Parent
        gui:Clone().Parent = game.Players:GetPlayerFromCharacter(driver).PlayerGui
    end
end)

seat.ChildRemoved:connect(function(removed)
    if removed.Name == "SeatWeld" then
        game.Players:GetPlayerFromCharacter(driver).PlayerGui:WaitForChild(gui.Name):Destroy()
    end
end)

Answer this question