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

I am trying to create a seat that you get out of by pressing a GUI button. Help with an issue?

Once you sit in the seat, a GUI button will pop up that once clicked, will teleport you out of the seat.

I am quite new to scripting so the code is pretty rough I know. The main issue i'm having, is that it teleports all of the players that have previously sat in the seat as well instead of just you.

It also uses a remote event to allow the gui to come onto the players screen via localscript.

The following is the code inside of a regular script in a normal Part that I am using for the seat. I'm not using an actual seat on purpose.

local seat = script.Parent


sitting = false
sat = game.ServerStorage.Sat.Value -- this is to make sure no one else currently is in the seat



seat.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local mouse = player:GetMouse()
		
	if sat == false then
	sat = true
	
	if sitting == false then
		humanoid.Sit = true
		sitting = true
		humanoid.Parent.Torso.Anchored = true
		wait(0.01)
		humanoid.Parent.Torso.CFrame = CFrame.new(-270.5, 4.5, -14)
		humanoid.Parent.Torso.Orientation = Vector3.new(0,180,0)
		game.ReplicatedStorage.RemoteEvent:FireClient(player) -- gui pops up
		game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function() -- button to get off the seat is pressed
			humanoid.Parent.Torso.Anchored = false
			humanoid.Sit = false
			wait()
			humanoid.Parent.Torso.CFrame = CFrame.new(-270.344, 3.997, -8.701)
			wait(0.5)
			sitting = false
			sat = false
			
		end)
		
end
end
end
end)

Sorry for the long question. Thanks for any help provided.

1 comment

0

This is a part right, not an actual seat?

2 answers

A few things with your script that will be causing you issues:

local mouse = player:GetMouse() The mouse is not accessible from the server, it only exists on the client. Though you don't actually use the mouse after this, so you can just take this line out.

sat = game.ServerStorage.Sat.Value and if sat == false then sat = true

This first line returns a BoolValue (true/false). It doesn't give you the property 'Value' of the BoolObject 'Sat', so when you try to change it with sat = true, you don't update the BoolObject, you only update that variable. This is what you want to do instead:

local sat = game.ServerStorage.Sat -- getting the BoolObject
...
if sat.Value == false then
	sat.Value = true -- now you're actually updating the BoolObject
--	etc...

I don't know if this will completely fix your script, but it's two things that jumped out to me when I read through it. If you have further problems let me know.

1 comment

0

Thank you that does help. But i think it has something to do with the script keeping all of the humanoids from everyone that has sat in the seat, and then it CFrames them all instead of just the local humanoid. Do you know I could stop this from happening?

seat script:

while true do
	wait(1)
if script.Parent:FindFirstChild("SeatWeld") then
	local player = game.Players:GetPlayerFromCharacter(script.Parent.SeatWeld.Part1.Parent)
	player.PlayerGui.ScreenGui.TextButton.Visible = true
end
end

gui script:

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.cars:FireServer()
end)

stand up script:

game.ReplicatedStorage.cars.OnServerEvent:Connect(function(player)
	for i,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("Weld") then
			if v.Part1.Name == "HumanoidRootPart" then
				local foundplayer = game.Players:GetPlayerFromCharacter(v.Part1.Parent)
				if foundplayer == player then
				v:Destroy()
				end
			end
		end
	end
end)

when you sit down a weld will always get created to make you stick to the seat, through that weld we can find the player from part1's parent and then after we get the player we enable their gui button. When the button is clicked it fires a remote event

in this remote event we check through the entire workspace after a weld when we find a weld we check if its parent name is humanoidrootpart, if that is correct we check if the player that occupied this seat is the same player who fired the remote event, if it is then we destroy that weld making the player get out of the seat

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