Please help me with my script. I get no errors or warnings. It was supposed to show a GUI when the local player enters the seat, but it doesn’t send the event… What did I do wrong? Please help me. My code is down below:
local player = game:GetService("Players").LocalPlayer
local seat = script.Parent
local PowerOnCarRadioEvent = game:GetService("ReplicatedStorage"):WaitForChild("CarRadio").PowerOnCarRadio
if seat.Occupant.Parent.Name == player.Name then
PowerOnCarRadioEvent:Fire(player)
print(player.Name.. " has entered a driver seat. Event successfully sent, powering on car radio.")
end
It only checks if there’s an occupant once. You have to continuously check to see if there’s an occupant, and then when there is, fire the event, and wait until the occupant gets out of the seat (the local player), then repeat this process.
wait(1) -- Not required
workspace.Seat.Changed:Connect(function()
if workspace.Seat.Occupant.Parent.Name == game.Players.LocalPlayer.Name then -- Change this reference to fit your game
print('Connect fired - ' .. game.Players.LocalPlayer.Name .. " entered Workspace's seat.")
-- Enable car radio gui here
repeat task.wait() until workspace.Seat.Occupant == nil
-- Disable car radio code here
end
end)
You should use this instead, the Connect event fires when the seat is entered, then you fire the car radio gui event. The script yields (waits) until occupant = nil, then you disable the car radio either using an event, or something else.
Also, the event won’t fire under a part because the LocalScript doesn’t run, itself. Put it under StarterCharacterScripts.
local player = game:GetService("Players").LocalPlayer
local seat = workspace. -- Reference the player's car seat here
local PowerOnCarRadioEvent = game:GetService("ReplicatedStorage"):WaitForChild("CarRadio").PowerOnCarRadio
seat.Changed:Connect(function()
if seat.Occupant ~= nil then -- Only fire event if you're in the seat
PowerOnCarRadioEvent:Fire(player) -- Make gui appear
print(player.Name.. " has entered a driver seat. Event successfully sent, powering on car radio.")
repeat wait() until seat.Occupant == nil -- Wait for you to get out of the seat
-- Disable the GUI here
end
end)
As the first person who replied asked, “Is it a RemoteEvent?”, the best way to handle this is to fire a RemoteEvent using :FireServer() from a client and receive the client event call from a script in Workspace or inside of the RemoteEvent. This way, you don’t need to fire a BindableEvent with LocalPlayer, and instead just use :FireServer() itself. If you use this, the client automatically fires the localplayer to the server itself, then you can change the PlayerGui of the player that fired the event, and turning on the GUI.
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local seat = workspace:WaitForChild("Seat")
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if seat.Occupant then
if seat.Occupant == humanoid then
--Play sound.
else
--Stop sound.
end
else
--Stop sound.
end
end)
Your script is going to error if seat.Occupant is nil.