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

How to make a ScreenGui element show upon seating on a seat?

Asked by 5 years ago

So I have a Script that fires a RemoteEvent whenever the player sits on a seat. It looks like this:

local cgpEvent = game.ReplicatedStorage.ChangeGUIPosition
local garage1 = game.Workspace.Garages.Garage1
local seat1 = garage1.LaptopTable1.Seat.Seat

seat1.Touched:Connect(function(plr)
    cgpEvent:FireClient(plr, 1)
end)

Then the LocalScript that receives the signal:

local guiEvent = game.ReplicatedStorage.ChangeGUIPosition
local seatTouched = false
local owns

if script.Parent.Parent.Name ~= "StarterGui" then
    owns = script.Parent.Parent.Parent.Value -- The Value is called 'Value'
end

guiEvent.OnClientEvent:connect(function(plr, seat)
    if seat == 1 and owns.Value == 1 then
        playerGui = script.Parent
        seatTouched = true
        playerGui.Hint:TweenPosition(UDim2.new(0.5, 0, 0.8, 0))
        playerGui.Hint2:TweenPosition(UDim2.new(0.5, 0, 0.85, 0))
        if seatTouched == true then
            wait(5)
            seatTouched = false
        end
        if seatTouched == false then
            playerGui.Hint:TweenPosition(UDim2.new(0.5, 0, 1.1, 0))
            playerGui.Hint2:TweenPosition(UDim2.new(0.5, 0, 1.15, 0))
        end
    end
end)

The problem is, the function doesn't work. It should give you a pop-up with a hint that you can press 'E' to open the menu, and it doesn't.

I tried putting 'print()' in the event in Script and it worked, but when I put the print() inside of the LocalScript' function it won't show up. It's probably because it's a LocalScript. I'll mark your answer as the answer if my problem is solved!

0
What are you trying to do in both 'if's of 'seatTouched' ? User#17685 0 — 5y
0
not really important Tymberlejk 143 — 5y
1
line 10: put seat.Value instead of seat and secondly, you shouldn't use .Touched as it isn't effecient. You should use this: seat1.ChildAdded:connect(function(child) if child:IsA("Weld") == true then cgpEvent:FireClient(plr, 1) end end) <- the seat creates a weld between the player and the seat when you sit down which totally helps in scripts like these. userdetail 1 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

The problem with your script is on the OnClientEvent. Look at this line:

guiEvent.OnClientEvent:Connect(function(plr, seat)

There is something wrong. It's the plr parameter. This is a common mistake with RemoteEvents and RemoteFunctions. Many users may know that the first parameter to OnServerEvent and OnServerInvoke is always the Player that fired/invoked the remote, or because the first argument to Invoke/FireClient is always the Player object to fire/invoke to. They pass it on to OnClient, but is not necessary, since the client doesn't need to know who fired the remote, because the client does NOT fire OnClientEvent. The server is firing the remote to the client.

guiEvent.OnClientEvent:Connect(function(seat)

-- Fixed!

Now another problem with your script is in your Touched event.

seat.Touched:Connect(function(plr)
    cgpEvent:FireClient(plr, 1)
end)

You're trying to fire the remote to a BasePart. You'll have to get the player on your own, the Touched event will not do this for you.

seat.Touched:Connect(function(part)
    local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)

    if plr then
        cgpEvent:FireClient(plr, 1)
    end
end)

Here's the final product:

Server code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local cgpEvent = ReplicatedStorage:WaitForChild("ChangeGUIPosition")
local garage1 = game.Workspace.Garages.Garage1
local seat1 = garage1.LaptopTable1.Seat.Seat

seat.Touched:Connect(function(part)
    local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)

    if plr then
        cgpEvent:FireClient(plr, 1)
    end
end)

Client code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local guiEvent = ReplicatedStorage:WaitForChild("ChangeGUIPosition")
local seatTouched = false
local owns

owns = script.Parent.Parent.Parent.Value -- The Value is called 'Value'

guiEvent.OnClientEvent:Connect(function(seat)
    if seat == 1 and owns.Value == 1 then
        playerGui = script.Parent
        seatTouched = true
        playerGui.Hint:TweenPosition(UDim2.new(0.5, 0, 0.8, 0))
        playerGui.Hint2:TweenPosition(UDim2.new(0.5, 0, 0.85, 0))
        if seatTouched then -- no need for == true!
            wait(5)
            seatTouched = false
        end
        if not seatTouched then -- No need for == false
            playerGui.Hint:TweenPosition(UDim2.new(0.5, 0, 1.1, 0))
            playerGui.Hint2:TweenPosition(UDim2.new(0.5, 0, 1.15, 0))
        end
    end
end)
0
I already answered my own question but I'll mark it as the answer. I appreciate your help. Tymberlejk 143 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Ok, I was playing with the script for a while and was discussing it with @userdetail on discord and now it works. Here's how the scripts look now:

Script:

local cgpEvent = game.ReplicatedStorage.ChangeGUIPosition
local garage1 = game.Workspace.Garages.Garage1
local seat1 = garage1.LaptopTable1.Seat.Seat

-- Changed the whole function so it doesn't happen too often thanks to @userdetail
seat1.ChildAdded:connect(function(child, plr)
    if child:IsA("Weld") == true then
        cgpEvent:FireClient(plr, 1)
    end
end)

LocalScript:

local guiEvent = game.ReplicatedStorage.ChangeGUIPosition
local seatTouched = false
local owns

if script.Parent.Parent.Name ~= "StarterGui" then
    owns = script.Parent.Parent.Parent:WaitForChild("Value") -- I've put a :WaitForChild() here
end

guiEvent.OnClientEvent:connect(function(seat) -- I deleted the 'plr' here
    if seat == 1 and owns.Value == 1 then
        playerGui = script.Parent
        seatTouched = true
        playerGui.Hint:TweenPosition(UDim2.new(0.5, 0, 0.8, 0))
        playerGui.Hint2:TweenPosition(UDim2.new(0.5, 0, 0.85, 0))
        if seatTouched == true then
            wait(5)
            seatTouched = false
        end
        if seatTouched == false then
            playerGui.Hint:TweenPosition(UDim2.new(0.5, 0, 1.1, 0))
            playerGui.Hint2:TweenPosition(UDim2.new(0.5, 0, 1.15, 0))
        end
    end
end)

EDIT: This script works only on Studio unfortunately.

0
That will error. The ChildAdded event only passes one parameter. You tried passing two. User#19524 175 — 5y
0
then how do you explain it works for me? Tymberlejk 143 — 5y

Answer this question