How to throw a ball?

Hello,

So I tried to make it so the player’s can throw balls but I don’t know how. So if someone could help me with this that would be awesome.

The ball does needs to have speed and gravity.

This is my ServerScript

local rep = game:GetService("ReplicatedStorage")
local event = rep:WaitForChild("CheckLine").CheckEvent



event.OnServerEvent:Connect(function(player, cframe)
	local ball = player.Backpack:FindFirstChild("DodgeBall") or player.Character:FindFirstChild("DodgeBall")
	print(ball)
	
	
	local handle = ball.Handle:Clone()
	handle.Parent = game.Workspace
	handle.Anchored = false
	handle.Name = "DodgeBall"
	print("worked")
	local linearVel = handle.LinearVelocity
	local allignPos = handle.AllignPosition
	
	linearVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	linearVel.VectorVelocity = (player.Character.HumanoidRootPart.lookVector*100)
	print("very noice")
	ball:Destroy()
	
	
	
	
end)

And this my localscript

local vel = script.Parent.LinearVelocity
local allignPos = script.Parent.AlignPosition

local uis = game:GetService("UserInputService")

local debounce = false
local tool = script.Parent.Parent





local rep = game:GetService("ReplicatedStorage")
local event = rep:WaitForChild("CheckLine"):FindFirstChild("CheckEvent")

uis.InputBegan:Connect(function(input, IsTyping)
	if not IsTyping then
		if not debounce then

			debounce = true

			tool.Equipped:Connect(function()
				if input.KeyCode == Enum.UserInputType.MouseButton1 then
					print("printStatement")
					event:FireServer()
					print("firedServer")
				end
			end)
		end
	end
end)
1 Like

You are setting the debounce to true and never back to false so your script is going to let you throw the ball only once

Also

This is the problem because tool.equipped is an event fired when someone equips the tool and you are listening to it only after the player already clicked and it’s not going to fire so it’s preventing the remote from firing. Remove that line if you want it to fire only if the tool is equipped that’s not the way you do it, instead use an equipped variable.

But please be more specifying on what is your problem

I can’t change the script rn but if I do will it let me throw the ball?

Correct the things I stated in the previous post and it should let you throw the ball. Is the remote firing because from what i could see the script would not fire the event

No with the script I had the event was not firing.

I was 50% right because if you listen to the equipped event after the mouse button is clicked you will prevent the event from firing because it will wait for the player to equip the tool in order to fire it. So with the logic you wrote that script it would have fired the event after you clicked the mouse button, unequipped and then re-equipped the tool. The other 50% was that you are setting the debounce to true after every button is pressed so despite it would not run for the previous reason then it wouldn’t because debounce will always be true

The script should look like this

--Do not declare variables you are not going to use 
local uis = game:GetService("UserInputService")

local debounce = false
local equipped = false
local tool = script.Parent.Parent

tool.Equipped:Connect(function()
     equipped = true
end)

tool.Unequipped:Connect(function()
     equipped = false
end)

local rep = game:GetService("ReplicatedStorage")
local event = rep:WaitForChild("CheckLine"):FindFirstChild("CheckEvent")

uis.InputBegan:Connect(function(input, IsTyping)
	if not IsTyping then
		if not debounce and equipped then
            if input.KeyCode == Enum.UserInputType.MouseButton1 then
                debounce = true --set the debounce here or else every button that is pressed will set it to true and prevent the script from executing
	            print("printStatement")
		        event:FireServer()
	            print("firedServer")
                task.wait(1)
                debounce = false --set the debounce to false or it will not run again
	        end
		end
	end
end)

Ok I changed the script to that. But it still doesn’t throws the ball. Nor does it prints what I have in the print statements. Also it is a localScript inside a tool. And ofcourse it is in the workspace but LocalScripts doesn’t work there so I think that’s the problem too.

Can you send your current script? LocalScripts don’t work in workspace but in a tool they do because they are parented to the player’s character

Ok I changed it back to a LocalScript and this is the current LocalScript:

local vel = script.Parent.LinearVelocity
local allignPos = script.Parent.AlignPosition

local uis = game:GetService("UserInputService")

local debounce = false
local tool = script.Parent.Parent





game.Players.PlayerAdded:Connect(function(player)
	tool.Equipped:Connect(function()
		tool.Activated:Connect(function()


			debounce = true




			local ball = player.Backpack:FindFirstChild("DodgeBall") or player.Character:FindFirstChild("DodgeBall")
			print(ball)
			local handle = ball.Handle:Clone()
			handle.Parent = game.Workspace
			handle.Anchored = false
			handle.Name = "DodgeBall"
			print("worked")
			local linearVel = handle.LinearVelocity
			local allignPos = handle.AllignPosition
			linearVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			linearVel.VectorVelocity = (player.Character.HumanoidRootPart.lookVector*100)
			print("very noice")
			ball:Destroy()



		end)
	end)
end)

You don’t need the player added event also because it doesn’t work in localscripts. You don’t need it because scripts load together with the player

Alright it kinda works right now but I get this error:

image

On line 32

LookVector is a property of CFrame not parts

Ok, now it works but the ball doesn’t get shots but it does deletes it from the backpack and stuff. But I need to to shoot. So there is something wrong with the linearVel.

Well, First you grab it, then you (understand throw) swing, step with the opposite foot, and throw. I hope this answers it.

1 Like

Uh… Well yea but the ball doesn’t moves it only goes to the ground but it doesn’t throws it cause somethign is wrong with the linearvelocity. Or allignposition I need that so it can simulate the throwing.

Could I just use raycast to check where the mouse is pointing at and then fire the shot with alignposition?

Alright I changed it to some raycasting but it still doesn’t fires it can someone please fix my script?

local vel = script.Parent.LinearVelocity
local allignPos = script.Parent.AlignPosition

local uis = game:GetService("UserInputService")

local debounce = false
local tool = script.Parent.Parent

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()



tool.Equipped:Connect(function()
	tool.Activated:Connect(function()


		debounce = true


		local ball = player.Backpack:FindFirstChild("DodgeBall") or player.Character:FindFirstChild("DodgeBall")
		print(ball)
		local handle = ball.Handle:Clone()
		handle.Parent = game.Workspace
		handle.Anchored = false
		handle.Name = "DodgeBall"
		handle.CanCollide = true
		print("worked")
		local linearVel = handle.LinearVelocity
		
		local rayOrigin = handle.Position
		local rayDirection = mouse.Hit.Position
		
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {mouse.Parent}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
		
		if raycastResult then
			local hitpart = raycastResult.Instance
			if hitpart.Parent:IsA("Folder") and hitpart.Transparency == 0 and not hitpart.Locked then
				hitpart.Locked = true
				
			end
			local attachment0 = Instance.new("Attachment")
			attachment0.Parent = raycastResult.Instance
			
			local allignPos = handle.AlignPosition
			allignPos.Attachment0 = attachment0
			allignPos.Attachment1 = script.Parent.Attachment
			
			allignPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			
			
			print("very noice")
			ball:Destroy()
		end
		
		
		
		




	end)
end)