So I have a Script that fires a RemoteEvent whenever the player sits on a seat. It looks like this:
1 | local cgpEvent = game.ReplicatedStorage.ChangeGUIPosition |
2 | local garage 1 = game.Workspace.Garages.Garage 1 |
3 | local seat 1 = garage 1. LaptopTable 1. Seat.Seat |
4 |
5 | seat 1. Touched:Connect( function (plr) |
6 | cgpEvent:FireClient(plr, 1 ) |
7 | end ) |
Then the LocalScript that receives the signal:
01 | local guiEvent = game.ReplicatedStorage.ChangeGUIPosition |
02 | local seatTouched = false |
03 | local owns |
04 |
05 | if script.Parent.Parent.Name ~ = "StarterGui" then |
06 | owns = script.Parent.Parent.Parent.Value -- The Value is called 'Value' |
07 | end |
08 |
09 | guiEvent.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(UDim 2. new( 0.5 , 0 , 0.8 , 0 )) |
14 | playerGui.Hint 2 :TweenPosition(UDim 2. new( 0.5 , 0 , 0.85 , 0 )) |
15 | if seatTouched = = true then |
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!
OnClientEvent
. Look at this line:1 | guiEvent.OnClientEvent:Connect( function (plr, seat) |
plr
parameter. This is a common mistake with RemoteEvent
s and RemoteFunction
s. 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.1 | guiEvent.OnClientEvent:Connect( function (seat) |
2 |
3 | -- Fixed! |
Touched
event.1 | seat.Touched:Connect( function (plr) |
2 | cgpEvent:FireClient(plr, 1 ) |
3 | end ) |
BasePart
. You'll have to get the player on your own, the Touched
event will not do this for you.1 | seat.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 |
7 | end ) |
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local cgpEvent = ReplicatedStorage:WaitForChild( "ChangeGUIPosition" ) |
03 | local garage 1 = game.Workspace.Garages.Garage 1 |
04 | local seat 1 = garage 1. LaptopTable 1. Seat.Seat |
05 |
06 | seat.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 |
12 | end ) |
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local guiEvent = ReplicatedStorage:WaitForChild( "ChangeGUIPosition" ) |
03 | local seatTouched = false |
04 | local owns |
05 |
06 | owns = script.Parent.Parent.Parent.Value -- The Value is called 'Value' |
07 |
08 | guiEvent.OnClientEvent:Connect( function (seat) |
09 | if seat = = 1 and owns.Value = = 1 then |
10 | playerGui = script.Parent |
11 | seatTouched = true |
12 | playerGui.Hint:TweenPosition(UDim 2. new( 0.5 , 0 , 0.8 , 0 )) |
13 | playerGui.Hint 2 :TweenPosition(UDim 2. new( 0.5 , 0 , 0.85 , 0 )) |
14 | if seatTouched then -- no need for == true! |
15 | wait( 5 ) |
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:
01 | local cgpEvent = game.ReplicatedStorage.ChangeGUIPosition |
02 | local garage 1 = game.Workspace.Garages.Garage 1 |
03 | local seat 1 = garage 1. LaptopTable 1. Seat.Seat |
04 |
05 | -- Changed the whole function so it doesn't happen too often thanks to @userdetail |
06 | seat 1. ChildAdded:connect( function (child, plr) |
07 | if child:IsA( "Weld" ) = = true then |
08 | cgpEvent:FireClient(plr, 1 ) |
09 | end |
10 | end ) |
LocalScript:
01 | local guiEvent = game.ReplicatedStorage.ChangeGUIPosition |
02 | local seatTouched = false |
03 | local owns |
04 |
05 | if script.Parent.Parent.Name ~ = "StarterGui" then |
06 | owns = script.Parent.Parent.Parent:WaitForChild( "Value" ) -- I've put a :WaitForChild() here |
07 | end |
08 |
09 | guiEvent.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(UDim 2. new( 0.5 , 0 , 0.8 , 0 )) |
14 | playerGui.Hint 2 :TweenPosition(UDim 2. new( 0.5 , 0 , 0.85 , 0 )) |
15 | if seatTouched = = true then |
EDIT: This script works only on Studio unfortunately.