Hi, I have been working on a game for some time now and I keep running into this issue. The bedpart (It is a Seat) keeps moving 5 studs away when you step out of a bed.
Script in bed:
local HitBox = script.Parent
local Vacant = true
local Seat = script.Parent.Parent.Seat
local BedEvent = game.ReplicatedStorage.Events.Global.ShowBedUI
local player
-- If player touched 'Hitbox' put them in bed.
HitBox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and Vacant == true then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
player = plr
Seat:Sit(hit.Parent:FindFirstChild("Humanoid"))
Vacant = false
BedEvent:FireClient(plr)
end
end)
-- Set Bed as Vacant when vacant
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if not Seat.Occupant then
Vacant = true
BedEvent.Parent.HideBedUI:FireClient(player)
Seat.CFrame = script.Parent.Parent.Parent.Configuration.SeatPOS.Value
end
end)
There is a UI button that makes you go out of bed, this is the script:
local player = game.Players.LocalPlayer
local Event = game.ReplicatedStorage.Events.Global.ShowBedUI
local Event2 = game.ReplicatedStorage.Events.Global.HideBedUI
-- If ShowBedUI is fired show bedUI
Event.OnClientEvent:Connect(function()
player.PlayerGui.JumpOutOfBed.Background.Visible = true
end)
-- If HideBedUI is fired hide bedUI
Event2.OnClientEvent:Connect(function()
player.PlayerGui.JumpOutOfBed.Background.Visible = false
end)
-- If actived button is pressed remove player from bed
player.PlayerGui.JumpOutOfBed.Background.ActivedButton.MouseButton1Click:Connect(function()
local PlrChar = game.Players[player.Name].Character
if PlrChar then
local Plr_POS_X = PlrChar.HumanoidRootPart.CFrame.X
local Plr_POS_Y = PlrChar.HumanoidRootPart.CFrame.Y
local Plr_POS_Z = PlrChar.HumanoidRootPart.CFrame.Z
local Hum = PlrChar:FindFirstChild("Humanoid")
if Hum then
Hum:ChangeState(Enum.HumanoidStateType.GettingUp)
PlrChar.HumanoidRootPart.CFrame = CFrame.new(Plr_POS_X + 5, Plr_POS_Y, Plr_POS_Z)
end
else
error("Fatal-Error: Code 2 | Player Char not found.")
end
end)
It is probably a timing issue between the seat.occupant changed event, the humanoid root part cframe being set, and the weld actually being destroyed.
You could try checking the weld is destroyed before moving the character.
if Hum then
if Hum.SeatPart then
Hum.SeatPart:FindFirstChild("SeatWeld"):Destroy()
end
Hum:ChangeState(Enum.HumanoidStateType.GettingUp)
PlrChar.HumanoidRootPart.CFrame = CFrame.new(Plr_POS_X + 5, Plr_POS_Y, Plr_POS_Z)
end
It is kinda fixed now, but now I have the problem that the player cant enter the bed again. I made a few changes to to script but it didn’t work:
New scripts:
local player = game.Players.LocalPlayer
local Event = game.ReplicatedStorage.Events.Global.ShowBedUI
local Event2 = game.ReplicatedStorage.Events.Global.HideBedUI
-- If ShowBedUI is fired show bedUI
Event.OnClientEvent:Connect(function()
player.PlayerGui.JumpOutOfBed.Background.Visible = true
end)
-- If HideBedUI is fired hide bedUI
Event2.OnClientEvent:Connect(function()
player.PlayerGui.JumpOutOfBed.Background.Visible = false
end)
-- If actived button is pressed remove player from bed
player.PlayerGui.JumpOutOfBed.Background.ActivedButton.MouseButton1Click:Connect(function()
local PlrChar = game.Players[player.Name].Character
if PlrChar then
local Plr_POS_X = PlrChar.HumanoidRootPart.CFrame.X
local Plr_POS_Y = PlrChar.HumanoidRootPart.CFrame.Y
local Plr_POS_Z = PlrChar.HumanoidRootPart.CFrame.Z
local Hum = PlrChar:FindFirstChild("Humanoid")
if Hum then
if Hum.SeatPart then
Hum.SeatPart:FindFirstChild("SeatWeld"):Destroy()
end
Hum:ChangeState(Enum.HumanoidStateType.GettingUp)
PlrChar.HumanoidRootPart.CFrame = CFrame.new(Plr_POS_X - 5, Plr_POS_Y, Plr_POS_Z)
end
player.PlayerGui.JumpOutOfBed.Background.Visible = false
game.ReplicatedStorage.Events.S1.SetBedVacant:FireServer()
else
error("Fatal-Error: Code 2 | Player Char not found.")
end
end)
and
local HitBox = script.Parent
local Vacant = true
local Seat = script.Parent.Parent.Seat
local BedEvent = game.ReplicatedStorage.Events.Global.ShowBedUI
local player
-- If player touched 'Hitbox' put them in bed.
HitBox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and Vacant == true then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
player = plr
Seat:Sit(hit.Parent:FindFirstChild("Humanoid"))
Vacant = false
BedEvent:FireClient(plr)
end
end)
-- Set Bed as Vacant when vacant
game.ReplicatedStorage.Events.S1.SetBedVacant.OnServerEvent:Connect(function()
Vacant = true
end)