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.
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)
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