Why isn't my code working?

So basically, everything in the code works except the remote event. I’m not a great coder so sorry if the code is really bad. I really need help!

local proximityPrompt = script.Parent
local seat = proximityPrompt.Parent
local isOccupied = false
local soundFolder = workspace.Sounds
local relaxedScene = soundFolder["Relaxed Scene"]
local soapLord = game.ReplicatedStorage["Soap Lord"]

local player


local function getPlayer()
	if seat.Occupant then
		player = game.Players:GetPlayerFromCharacter(seat.Occupant)
	end
end

getPlayer()

local function handleSit(player)
	if not isOccupied then
		seat:Sit(player.Character.Humanoid)
	end
end

proximityPrompt.Triggered:Connect(handleSit)

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant then
		seat.Disabled = false
		isOccupied = true
		proximityPrompt.Enabled = false
		task.wait(4)
		relaxedScene:Play()

		if relaxedScene.IsPlaying == true then
			task.wait(15)
			relaxedScene:Pause()
			game.ReplicatedStorage["Soap Lord"].Parent = workspace
			soapLord:MoveTo(Vector3.new(-557.975, -1.999, 66.771))
			soundFolder["Minecraft Cave Sound #1"]:Play()
			task.wait(3)
			soapLord.SoapBody.gun.Transparency = 0
			soundFolder["Gun Reload"]:Play()
			wait(1)
			soundFolder["Oh - hell"]:Play()
			wait(2)
			local plr = game:GetService("Players").PlayerAdded:Wait()
			game.ReplicatedStorage.Endings.SoapLord:FireAllClients(plr,  "THE SOAP LORD",  "He has drip though.")
		end
	else
		isOccupied = false
		proximityPrompt.Enabled = true
	end
end)
4 Likes

Try using a print statement where the remote event is scripted at the very start of the code to check if its firing it, if it is printing it, it’s something wrong with that code. Otherwise try changing the variables on the remote event

I’m not sure what your purpose is here at all, but this line yields the script until a new player joins the server.

1 Like

I completely overlooked that! :rofl:

1 Like

yeah idk what to do with that line.

It obviously depends on what your intention is with the remote event, so I didn’t bother proposing a solution. Could you clarify what the plr variable is supposed to be and why you pass it into :FireAllClients()?

I tried using it cause i thought it would make a difference but it didn’t

also it gave me an error FireClient: player argument must be a Player object but now since i changed the plr variable it didn’t give this error anymore

Could you tell us the current issue? :FireAllClients() (iirc) should never require a player as an argument because the method fires to all players in the server. :FireClient() will definitely demand a player since it sends information to said player’s client.

so i dont need the plr variable? also the current issue is that everything else runs in the code except the lines with the remote event

Please tell me what the remote event’s purpose is so I can determine whether you need a target player or not. You are using :FireAllClients() yet trying to instantiate a variable for a player which may or may not be intentional.

it shows a gui i can send u the code for it if u want

To whom are you sending the interface? :FireAllClients() is probably your correct call if you want to show the interface to everybody, but :FireClient() will definitely work nicer if you only want to show the seat’s occupant.

local tweenservice = game:GetService("TweenService")
local replicatedservice = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local events = replicatedservice.Endings
local gui = script.Parent
local frame = gui.BlackScreen
local player = players.LocalPlayer
local ending = events.SoapLord
local sound = game.Workspace.Sounds["UnderTale - Text Sound(Fixed)"]
local char = player.Character
local hum = char:WaitForChild("Humanoid")



local function typeText(text)
	for i = 1, #text do
		script.Parent.BlackScreen.Ending.Text = string.sub(text, 1, i)
		sound:Play() 
		wait(0.04)
	end
end

local function typeText2(text)
	for i = 1, #text do
		script.Parent.BlackScreen.Text.Text = string.sub(text, 1, i)
		sound:Play()
		wait(0.04)
	end
end


ending.OnClientEvent:Connect(function(endingname, desc)
	frame.Visible = true
	local tweenframe = tweenservice:Create(frame, TweenInfo.new(2), {BackgroundTransparency = 0})
	hum.WalkSpeed = 0
	tweenframe:Play()
	wait(2)
	typeText("Ending")
	wait(3)
	typeText2(endingname)
	wait(4)
	player:Kick(desc)
end)

this is the other script

by the way its a 1 player game and i already tried fire client and it still has the same issue.

If you are communicating with the player from the server to show an interface when he/she becomes an occupant, :FireAllClients() will function correctly, but you should use :FireClient() for good practice.

It is important to note that the occupant of a seat can be something other than a player. To determine whether the occupant is an (or ‘the’ in your case) actual player, we can use :GetPlayerFromCharacter()'s result.

If the result is nil, simply escape from the code. If the result is a player, fire the remote event for that specific player:

local plrs = game:GetService'Players';
local remoteEvent: RemoteEvent; --consider making your remoteevent its own variable so the script doesnt have to index replicatedstorage every time (and also incase the remoteevent somehow changes ancestry)

--on occupant changed (your signal)
local plr = seat.Occupant and plrs:GetPlayerFromCharacter(seat.Occupant.Parent); --if seat.Occupant is nil, dont bother calling getplayerfromcharacter because it will fail regardless (plr will be nil)
--i should note that the parent of a humanoid is always the character(model) for all default roblox characters

if not plr then return; end --escape from the code immediately if plr doesnt exist

--whatever needs to be done
remoteEvent:FireClient(plr, ...); --tuple being your arguments you want to send

Thank you so much! I was stuck on this issue for days.

No problem! Just make sure you thoroughly understand what that code does instead of aimlessly integrating it. If you aren’t sure about something, I can probably clarify.

Oh, and also make sure to properly mark the reply as the solution.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.