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 6 years ago

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

1local cgpEvent = game.ReplicatedStorage.ChangeGUIPosition
2local garage1 = game.Workspace.Garages.Garage1
3local seat1 = garage1.LaptopTable1.Seat.Seat
4 
5seat1.Touched:Connect(function(plr)
6    cgpEvent:FireClient(plr, 1)
7end)

Then the LocalScript that receives the signal:

01local guiEvent = game.ReplicatedStorage.ChangeGUIPosition
02local seatTouched = false
03local owns
04 
05if script.Parent.Parent.Name ~= "StarterGui" then
06    owns = script.Parent.Parent.Parent.Value -- The Value is called 'Value'
07end
08 
09guiEvent.OnClientEvent:connect(function(plr, seat)
10    if seat == 1 and owns.Value == 1 then
11        playerGui = script.Parent
12        seatTouched = true
13        playerGui.Hint:TweenPosition(UDim2.new(0.5, 0, 0.8, 0))
14        playerGui.Hint2:TweenPosition(UDim2.new(0.5, 0, 0.85, 0))
15        if seatTouched == true then
View all 24 lines...

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 — 6y
0
not really important Tymberlejk 143 — 6y
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 — 6y

2 answers

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

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

1guiEvent.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.

1guiEvent.OnClientEvent:Connect(function(seat)
2 
3-- Fixed!

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

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

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.

1seat.Touched:Connect(function(part)
2    local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
3 
4    if plr then
5        cgpEvent:FireClient(plr, 1)
6    end
7end)

Here's the final product:

Server code:

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local cgpEvent = ReplicatedStorage:WaitForChild("ChangeGUIPosition")
03local garage1 = game.Workspace.Garages.Garage1
04local seat1 = garage1.LaptopTable1.Seat.Seat
05 
06seat.Touched:Connect(function(part)
07    local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
08 
09    if plr then
10        cgpEvent:FireClient(plr, 1)
11    end
12end)

Client code:

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local guiEvent = ReplicatedStorage:WaitForChild("ChangeGUIPosition")
03local seatTouched = false
04local owns
05 
06owns = script.Parent.Parent.Parent.Value -- The Value is called 'Value'
07 
08guiEvent.OnClientEvent:Connect(function(seat)
09    if seat == 1 and owns.Value == 1 then
10        playerGui = script.Parent
11        seatTouched = true
12        playerGui.Hint:TweenPosition(UDim2.new(0.5, 0, 0.8, 0))
13        playerGui.Hint2:TweenPosition(UDim2.new(0.5, 0, 0.85, 0))
14        if seatTouched then -- no need for == true!
15            wait(5)
View all 23 lines...
0
I already answered my own question but I'll mark it as the answer. I appreciate your help. Tymberlejk 143 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 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:

01local cgpEvent = game.ReplicatedStorage.ChangeGUIPosition
02local garage1 = game.Workspace.Garages.Garage1
03local seat1 = garage1.LaptopTable1.Seat.Seat
04 
05-- Changed the whole function so it doesn't happen too often thanks to @userdetail
06seat1.ChildAdded:connect(function(child, plr)
07    if child:IsA("Weld") == true then
08        cgpEvent:FireClient(plr, 1)
09    end
10end)

LocalScript:

01local guiEvent = game.ReplicatedStorage.ChangeGUIPosition
02local seatTouched = false
03local owns
04 
05if script.Parent.Parent.Name ~= "StarterGui" then
06    owns = script.Parent.Parent.Parent:WaitForChild("Value") -- I've put a :WaitForChild() here
07end
08 
09guiEvent.OnClientEvent:connect(function(seat) -- I deleted the 'plr' here
10    if seat == 1 and owns.Value == 1 then
11        playerGui = script.Parent
12        seatTouched = true
13        playerGui.Hint:TweenPosition(UDim2.new(0.5, 0, 0.8, 0))
14        playerGui.Hint2:TweenPosition(UDim2.new(0.5, 0, 0.85, 0))
15        if seatTouched == true then
View all 24 lines...

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 — 6y
0
then how do you explain it works for me? Tymberlejk 143 — 6y

Answer this question