Bodyvelocity wont disconnect

so im trying to make it so that the block will throw but bodyvelocity wont remove

local RS = game:GetService("ReplicatedStorage")

local greenblock = RS:WaitForChild("FlyingBlock")

local throwevent = RS:WaitForChild("ThrownBlock")

throwevent.OnServerEvent:Connect(function(player: Player ,holdingblockpos, newblock, mouseHit)
	
	newblock = greenblock
	newblock:Clone()
	newblock.Parent = workspace
		
		newblock.Position = holdingblockpos.Position
		local force = 35
		local BodyVelocity = Instance.new('BodyVelocity', newblock)
		BodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		BodyVelocity.Velocity = (mouseHit - holdingblockpos.Position).Unit * force + Vector3.new(0,(force / 2),0)
		BodyVelocity.P = 500
		task.wait(0.1)
		BodyVelocity:Remove()
		
		
end)	

Remove is deprecated, you should use :Destroy() instead, and you can also use debris service for removing instances at a scheduled time, like this

local debris = game:GetService("Debris"):
debris:AddItem(instance,secondsUntilRemove)

you can see debris service docs here

2 Likes
  1. BodyVelocity is also deprecated. You should instead use LinearVelocity or VectorForce.
  2. You need to set newblock to greenblock:Clone()
  3. You should not set the parent of an instance using the second parameter of Instance.new()

Here is the updated code.

local RS = game:GetService("ReplicatedStorage")

local greenblock = RS:WaitForChild("FlyingBlock")

local throwevent = RS:WaitForChild("ThrownBlock")

throwevent.OnServerEvent:Connect(function(player: Player ,holdingblockpos, newblock, mouseHit)

	newblock = greenblock:Clone()
	newblock.Parent = workspace

	newblock.Position = holdingblockpos.Position
	
	local attachment = Instance.new("Attachment")
	attachment.Parent = newblock
	attachment.CFrame = CFrame.new(0,0,0)
	
	local force = 35
	local LinearVelocity = Instance.new('LinearVelocity')
	LinearVelocity.MaxForce = math.huge
	LinearVelocity.VectorVelocity = (mouseHit - holdingblockpos.Position).Unit * force + Vector3.new(0,(force / 2),0)
	LinearVelocity.Attachment0 = attachment
	LinearVelocity.Parent = newblock
	
	task.delay(0.1, function()
		LinearVelocity:Destroy()
	end)
	-- or use debris
	-- game.Debris:AddItem(LinearVelocity, 0.1)
end)

Please don’t do game.Service and please don’t use DebrisService as a whole (bc it has a part limit), instead do this;

task.delay(5, function()
 part:Destroy()
end)
2 Likes

Fixed.

1 Like

This also has a potential drawback in that the scheduled callback will never run if the script is disabled or destroyed before the callback runs. Whereas DebrisService runs outside the context of the script, guaranteeing the instance will eventually be destroyed even if the script is disabled or destroyed. For the posters specific problem, DebrisService is the best option

1 Like

it does work and doesnt float now, but it just drops and doesnt throw with force

BodyVelocity is fine to use, unlike Remove which parents the instance and all children to nil. I’m sure you see how that’s a bad thing

1 Like

Yeah, but it still is unpreferable. You shouldn’t use it if you don’t have to.

well BodyVelocity is like 5 times easier to use than LinearVelocity so…

You just need an attachment for LinearVelocity… it’s not that much harder.

1 Like

You could try increasing your force, increasing the time before destroying the body velocity or try a mix

this is what i have rn:

local RS = game:GetService("ReplicatedStorage")

local greenblock = RS:WaitForChild("FlyingBlock")

local throwevent = RS:WaitForChild("ThrownBlock")

throwevent.OnServerEvent:Connect(function(player: Player ,holdingblockpos, newblock, mouseHit)

	newblock = greenblock:Clone()
	newblock.Parent = workspace

	newblock.Position = holdingblockpos.Position

	local attachment = Instance.new("Attachment")
	attachment.Parent = newblock
	attachment.CFrame = CFrame.new(0,0,0)

	local force = 1000
	local LinearVelocity = Instance.new('LinearVelocity')
	LinearVelocity.MaxForce = math.huge
	LinearVelocity.VectorVelocity = Vector3.new((mouseHit - holdingblockpos.Position).Unit * force + Vector3.new(0,(force / 2),0))
	LinearVelocity.Attachment0 = attachment
	LinearVelocity.Parent = newblock

	task.delay(0.1, function()
		LinearVelocity:Destroy()
	end)
end)

--(mouseHit - holdingblockpos.Position).Unit * force + Vector3.new(0,(force / 2),0)

but it says:

ServerScriptService.BlockManager:21: attempt to perform arithmetic (sub) on nil and Vector3 - Server - BlockManager:21

i did it

local force = 50
	local BodyVelocity = Instance.new("BodyVelocity", newblock)
	BodyVelocity.MaxForce = Vector3.new(1, 1, 1) * 10000000;
	BodyVelocity.Velocity = newblock.CFrame.UpVector * force;

	debris:AddItem(BodyVelocity, 0.1)

thanks for anyone who helped!

1 Like

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