2 Items are Destroying only Once

I have a seat part that i use to make a Bench Press system with, it has a prox prompt which clones a weights model and makes a motor6d weld. My issue is that i have a Script inside of StarterCharacterScripts and its purpose is to delete the motor6d weld and weights model when the player jumps out of the seat, it almost works, but it only destroys the motor6d and weights model once and doesnt destroy it after that.

Script:

local UserInputService = game:GetService("UserInputService")

local seats = {
	game.Workspace:WaitForChild("BenchSeat1")
}

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local rightHand = character:WaitForChild("RightHand")
local sitting = false
local debounce = false

--stuff to find
local motor6D = rightHand:WaitForChild("BenchpressMotor6D")
local weightModel = rightHand:WaitForChild("BasicBenchPressWeights")

UserInputService.JumpRequest:Connect(function()
	motor6D:Destroy()
	weightModel:Destroy()
end)

humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function()
	if table.find(seats, humanoid.SeatPart) then
		sitting = true
	else
		sitting = false
	end	
end)

Hi! When and where are you adding the motor6d and weights model to the player?

Hello!
Let me send you that script

local BenchPressWeights = game.ServerStorage:WaitForChild("BasicBenchPressWeights")
local Prox = script.Parent

Prox.Triggered:Connect(function(player)
	local character = player.Character
	if not character then
		return
	end
	local humanoid = character:WaitForChild("Humanoid")

	local rightHand = character:FindFirstChild("RightHand")

	if not rightHand then
		return
	end
	workspace.BenchSeat1:Sit(humanoid)
	local clone = BenchPressWeights:Clone()
	clone.Parent = rightHand

	local rightGrip = rightHand:FindFirstChild("RightGrip")
	if rightGrip then
		rightGrip:Destroy()
	end

	local Motor6D = Instance.new("Motor6D", rightHand)
	Motor6D.Name = "BenchpressMotor6D"
	Motor6D.Part0 = rightHand
	Motor6D.Part1 = clone.Handle
	Motor6D.C0 = CFrame.new(-1.62, 0, 0.03)
end)
1 Like

Problems You’ve Encountered

Okay, first of all, I’m assuming the StarterCharacterScripts script is a local script, which is the first problem. Although the part may be deleting, it is only on the local side. I advise you to move the script somewhere on the server, or fire a remote event and receive it on the script you just sent to delete the Motor6D and Weight Model there (I will cover how to do this later.)

Second, I recommend using Humanoid.Jumping instead of User Input Service for this.

Now here is where the problems come in. Using WaitForChild will yield the script until it can find the item it is looking for. If it can’t find the item, eventually it will time out since you have not specified the TimeOut parameter. For what you are trying to fulfill, I believe this is a bad way to achieve it.

Why it was only firing once

The reason your script was only firing once was because you sat in the seat the first time soon enough before the TimeOut happened. Variables are only established once in the beginning of the script. After deleting the items, WaitForChild doesn’t continue to search for them, so it just stops working.

More Information on WaitForChild Here

Instance | Documentation - Roblox Creator Hub

Approaching the Problem
Now to actually solve the problem, using what I told you, you can create something like this

local UserInputService = game:GetService("UserInputService")

local seats = {
	game.Workspace:WaitForChild("BenchSeat1")
}

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local rightHand = character:WaitForChild("RightHand")
local sitting = false
local debounce = false

humanoid.Jumping:Connect(function(isJumping)
        -- looking for the items
	local motor6D = rightHand:FindFirstChild("BenchpressMotor6D")
	local weightModel = rightHand:FindFirstChild("BasicBenchPressWeights")
        -- even if the items aren't found, the whole script shouldn't stop working in the future
	if motor6D and weightModel then -- actually checks if character has items
        -- destroying items if player doesn't have them
		weightModel:Destroy()
		motor6D:Destroy()
	end
end)

humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function()
	if table.find(seats, humanoid.SeatPart) then
		sitting = true
	else
		sitting = false
	end	
end)

Notice how this time I check if the player has the motor6D and weight model every time they jump instead of assuming they will have it with WaitForChild. As I mentioned earlier, this is local sided, so the items will only be destroyed on the client, which is very bad, so I will show you how to use a remote event to destroy the items on the server side.

Using Remote Event (Highly Recommended)

Local Script

local UserInputService = game:GetService("UserInputService")

local seats = {
	game.Workspace:WaitForChild("BenchSeat1")
}

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local rightHand = character:WaitForChild("RightHand")
local sitting = false
local debounce = false

humanoid.Jumping:Connect(function(isJumping)
	local motor6D = rightHand:FindFirstChild("BenchpressMotor6D")
	local weightModel = rightHand:FindFirstChild("BasicBenchPressWeights")
	if motor6D and weightModel then
		SomeEvent:FireServer() -- fires event to server instead destroying them here
	end
end)

humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function()
	if table.find(seats, humanoid.SeatPart) then
		sitting = true
	else
		sitting = false
	end	
end)

Server Script

SomeEvent.OnServerEvent:Connect(function(player) -- firing to server automatically gives player parameter
	local character = player.Character
	local rightHand = character:FindFirstChild("RightHand")
	
	local motor6D = rightHand:FindFirstChild("BenchpressMotor6D")
	local weightModel = rightHand:FindFirstChild("BasicBenchPressWeights")
	
	motor6D:Destroy()
	weightModel:Destroy()
end)

You can also do this by passing the objects as arguments on the remote event like this:
Targeted Section of Local Script

humanoid.Jumping:Connect(function(isJumping)
	local motor6D = rightHand:FindFirstChild("BenchpressMotor6D")
	local weightModel = rightHand:FindFirstChild("BasicBenchPressWeights")
	if motor6D and weightModel then
		SomeEvent:FireServer(motor6D, weightModel)
	end
end)

Server Script

SomeEvent.OnServerEvent:Connect(function(player, motor6D, weightModel)
    motor6D:Destroy()
    weightModel:Destroy()
end)

I prefer the first method, incase you need to do anything to the character or the objects before destroying. Make sure to actually replace “SomeEvent” with a remote event if you do this. As for the Server Script portion, you can just add this to the same script where you add the objects into the character.

If you have any questions about anything, let me know.

Wow, i am shocked, you took all this time to help me with a small issue, thank you. I went with the option without the need of a RemoteEvent and it works as a intended, Thank you for your time!

1 Like

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