Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script that's supposed to seat players puts multiple people in one seat?

The below script is intended to put players in a seat if it is not already occupied. I have boolValues inside their corresponding parts, however when I try to test this it teleports me and someone else to seat1. Please help!

script.Parent.MouseButton1Click:Connect(function()
	player = game.Players.LocalPlayer
	--Makes it so the player can't reset once boarded
	local function noRset()
			noReset = game.Workspace.NoReset:Clone()
			noReset.Parent = player:WaitForChild("PlayerGui")
	end
	print("hi")
	if game.Workspace.seat1.Occupied.Value == false then
		player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(1.3, 0.5, 9.4))
		noRset()
		game.Workspace.seat1.Occupied.Value = true
	elseif game.Workspace.seat2.Occupied.Value == false then
		player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(8.4, 0.5, 9.8))
		noRset()
		game.Workspace.seat2.Occupied.Value = true
	elseif game.Workspace.seat3.Occupied.Value == false then
		player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-23, 0.5, 7.4))
		noRset()
		game.Workspace.seat3.Occupied.Value = true
	elseif game.Workspace.seat4.Occupied.Value == false then
		player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-11.1, 0.5, 8.6))
		noRset()
		game.Workspace.seat4.Occupied.Value = true
	end
end)

2 answers

✓ Accepted answer

well, you doing it in a LocalScritpt, therefore, the seat.Occupied property only changes on one player's screen, but not other players. you would need to use remote events to update the seat.Occupied

ex: **step 1: put remote event somewhere in replicated storage

step 2: put a server script somewhere in ServerScriptService

step 3: put a LocalScript somewhere, where it will be cloned to be a descendant of player object**

In Server Script

local updateValueRE = game.ReplicatedStorage.UpdateValueRemoteEvent
local seats = {}
local seat_value = 1

while true do
	local seat = workspace[tostring("seat"..seat_value)]
	if not seat then
		break
	end
	seat_value = seat_value+1
	
	if seat.Classname:lower() == "seat" then
		table.insert(seats,#seats+1,seat)
	end
	wait(0.1)
end

updateValueRE.OnServerEvent:Connect(function(player)
	local character = player.Character

	if not character then
		return 0;
	end

	for _, seat in pairs(seats) do
		if not  seat.Occupant then
			Character:WaitForChild("HumanoidRootPart").CFrame = seat.CFrame
		end
	end
end)

**in local script ** make sure the script is paranted to the pressable GUI: AKA a button

script.Parent.MouseButton1Click:Connect(function()
	local updateValueRE = game.ReplicatedStorage.UpdateValueRemoteEvent
	updateValueRE:FireServer()
end)

Now this is just an example, but you can do it differently

1 comment

0

i don't understand what any of that code does

okay i saw that the above person mentioned an "occupant" property that i didn't notice so i incorporated that into my script by adding an "and not game.Workspace.seat1.Occupant" after the boolValue check. thanks !!

This discussion is preserved as it was. New answers, comments, and votes are closed.