How could i go about making a better throwing system

I’m trying to make it so that the blocks can be thrown separately instead of it being one part only, and have their different forces when throw

Example:

but my code can’t do so

Local Script inside Tool:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid: Humanoid = character:WaitForChild("Humanoid")
local event = game.ReplicatedStorage.ThrownBlock

local mouse = player:GetMouse()
local userservice = game:GetService("UserInputService")

local animator: Animator = humanoid.Animator

local animas = tool.Animations

local anims = {
	
	holding = animator:LoadAnimation(animas.Holding),
	throwing = animator:LoadAnimation(animas.Throw)
	
}

local debounce = true
anims.throwing.Priority = Enum.AnimationPriority.Action4

tool.Equipped:Connect(function()
	anims.holding:Play(.6)
end)

tool.Unequipped:Connect(function()
	anims.holding:Stop()
end)

tool.Activated:Connect(function()
		if not debounce then return end
		debounce = false
		anims.throwing:Play()
		task.wait(.67)
	event:FireServer(tool.Handle, mouse.Hit.Position)
		tool:Destroy()

		anims.holding:Stop()
	end)

Server Script Manager:

local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")

local newblocks = SS:WaitForChild("ThrowingBlocks"):GetChildren() 

local throwevent = RS:WaitForChild("ThrownBlock")

local debris = game:GetService("Debris")

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

	newblock = newblocks[1]:Clone()
	newblock.Parent = workspace

	newblock.Position = holdingblockpos.Position
	newblock.Rotation = holdingblockpos.Rotation

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

	debris:AddItem(BodyVelocity, 0.1)
	
	end)

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

help will be appreciated

5 Likes

My first suggestion in the localscript is to replace :GetService("Players") with just .Players since it’s easier to type and is faster to load.

Your localscript doesn’t use input service so you can remove this

Instead of creating the blocks and putting them into serverstorage, you can use Instance.new("BasePart") in the server script and set the properties directly in that script so you can save space in serverstorage and create blocks just by using the script. I would recommend making the block creation in a separate function used by the connection.
Here’s an example I made:

function makepart()
	local part = Instance.new("BasePart")
	part.Name = "block"
	part.Size = Vector3.new(3,3,3)
	part.Position = CFrame.new() -- whatever you want the position to be
	part.BrickColor = BrickColor.new("Really red") -- whatever color you want
	part.Parent = workspace
	part.CanCollide = false
	
	-- return the created block
	return part
end

local newpart = makepart()

I’m not exactly sure how debris is supposed to be used in this, but I assume you use it to make the part fall after 0.1 seconds? If that’s the case then you can make the BodyVelocity point upward for about a second or two and then destroy the velocity, which will make the ball start falling using gravity.

Just to be clear, I don’t know the entire use of this since the explanation was kind of short, but I hope this helped

3 Likes

i can’t use this because im also making custom animations for the parts when thrown and I’d also like to make other types of blocks and add them

i also use the debris to stop it from moving continuesly and make it stop by gravity like you said.

here a video:

2 Likes

Don’t use deprecated instances.
use :ApplyImpulse instead.

3 Likes

i works, but how would i also go about changing the direction the block is being thrown to

1 Like

You specify it in ApplyImpulse
It applies impulse using world coordinates for direction.

1 Like

i got it to work it was an attempt to perform arithmetic (mul) on nil and number error

1 Like

but i also wanted it to function for the other blocks aswell

1 Like

It has to be a vector/Vector3, look at documentation

1 Like

no, no i told you i got it to work thanks! now to turn it into a system for other blocks.

might figure it out on my own later idk

You don’t need debris, just use Instance:Destroy().
If each block has its own animation you can store them in serverstorage if you wish, however for the animations you should make a string attribute for each block and set it to its animation ID so the script can :GetAttribute() and load the ID from that string.

yeah im not using debris anymore :ApplyImpulse works

1 Like

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