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!
OnClientEvent
. Look at this line: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.guiEvent.OnClientEvent:Connect(function(seat) -- Fixed!
Touched
event.seat.Touched:Connect(function(plr) cgpEvent:FireClient(plr, 1) end)
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)
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)
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)
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.