Tool does something when 1 parameter is met, even though there are 2 parameters

The script is supposed to make it so that the tool fires out a ball when you are touching a box and when the player clicks. BUT, it works if i’m just touching the box and not clicking
robloxapp-20220507-1816455.wmv (560.0 KB)
Sorry its a download link

script:
Its a local script in the tool

local player = game.Players.LocalPlayer
local Racket = player.Character.Racket
local ball = game.Workspace.ball
local tool = script.Parent
local canshoot = true
--local boomy = script.Parent:WaitForChild("BallBoom")
local event = game.ReplicatedStorage.RacketEvents.RacketClient

function OnActivation()
	
	
	ball.Touched:connect(function(hit) 
		if hit:IsA("Part")  and tool.Activated and hit.Parent == Racket  then


			event:FireServer()
			if canshoot == true then
				canshoot = false
				print('Activation works')
				--event:FireServer(boomy)
				event:FireServer()


				wait(1)
				canshoot = true

			end




		end

	end)

end


tool.Activated:Connect(OnActivation)

Edit: It seems that the script works properly at first, but after you hit the box and click, the issue above happens. Do i have to add a loop or something?

Try disconnecting the previous touched connection:

local player = game.Players.LocalPlayer
local Racket = player.Character.Racket
local ball = game.Workspace.ball
local tool = script.Parent
local canshoot = true
--local boomy = script.Parent:WaitForChild("BallBoom")
local event = game.ReplicatedStorage.RacketEvents.RacketClient
local con = nil

function OnActivation()
	if con ~= nil then con:Disconnect() end
	
	con = ball.Touched:connect(function(hit) 
		if hit:IsA("Part")  and tool.Activated and hit.Parent == Racket  then


			event:FireServer()
			if canshoot == true then
				canshoot = false
				print('Activation works')
				--event:FireServer(boomy)
				event:FireServer()


				wait(1)
				canshoot = true

			end




		end

	end)

end


tool.Activated:Connect(OnActivation)

Didn’t work. Same results.


First of all, your script is connecting a function every single time the player clicks their mouse with the tool out. This will lead to massive lag and memory leak. Do not connect functions in an event unless you know what you are doing.

Second, .Activated fires whenever the player clicks with the tool out, so your 2nd parameter is always true.
Also note, you have tool.Activated in your script which will always be true since you are checking if the Activated Event exists, NOT if the player clicked.

Your .Touched function is the problem here. The proper way to tell if a part is touching another for an instant is to use OverlapParams and :GetPartsInPart.

local overlapParams = OverlapParams.new()
overlapParams.FilterDescendantsInstances = {ball} -- Detect ball
overlapParams.FilterType = Enum.RaycastFilterType.Whitelist -- Only detect in filter list

function OnActivation()
	local parts = workspace:GetPartsInPart(racket, overlapParams)
	
	for _, v in ipairs(parts) do
		if hit.Parent == racket then -- This part is actually unnecessary since only that one ball from FilterDescendantsInstances will ever be able to be detected by the function
			-- Code
		end
	end
end


tool.Activated:Connect(OnActivation)

Cool thanks.
Sorry I’m still not that advanced at scripting. ¯_(ツ)_/¯

I re wrote it like this:

local player = game.Players.LocalPlayer
local Racket = player.Character.Racket
local ball = game.Workspace.ball
local tool = script.Parent
local canshoot = true
--local boomy = script.Parent:WaitForChild("BallBoom")
local event = game.ReplicatedStorage.RacketEvents.RacketClient
local con = nil


local overlapParams = OverlapParams.new()
overlapParams.FilterDescendantsInstances = {ball} -- Detect ball
overlapParams.FilterType = Enum.RaycastFilterType.Whitelist -- Only detect in filter list


		
		
	function OnActivation()
		if con ~= nil then con:Disconnect() end

		con = ball.Touched:connect(function(hit) 


			local parts = workspace:GetPartsInPart(Racket, overlapParams)

			for _, v in ipairs(parts) do
			event:FireServer()
			if canshoot == true then
				canshoot = false
				print('Activation works')
				--event:FireServer(boomy)
				event:FireServer()


				wait(1)
				canshoot = true

			end
			end
	end)
	end


	
tool.Activated:Connect(OnActivation)



I get the error; Part parameter must be PartInstance - Client - BallSpawnClient:24

is this because the racket is a tool?

The script you need is:


local player = game.Players.LocalPlayer
local Racket = player.Character.Racket
local ball = game.Workspace.ball
local tool = script.Parent
local canshoot = true
local event = game.ReplicatedStorage.RacketEvents.RacketClient

local overlapParams = OverlapParams.new()
overlapParams.FilterDescendantsInstances = {ball} -- Detect ball
overlapParams.FilterType = Enum.RaycastFilterType.Whitelist -- Only detect in filter list

function OnActivation()
	local parts = workspace:GetPartsInPart(racket, overlapParams)
	
	for _, v in ipairs(parts) do
		if hit.Parent == racket then -- This part is actually unnecessary since only that one ball from FilterDescendantsInstances will ever be able to be detected by the function
			-- Code
		end
	end
end


tool.Activated:Connect(OnActivation)

It’s because you are passing the Racket tool when the function needs a BasePart. Pass the part that you want to use for detection, such as the tool.Handle.

1 Like

I changed it to racket.Base, a part of the racket. There are no output errors but it still doesn’t work properly.

Can you show your full code? I’m not sure where the problem is. Here’s a checklist you could go over:

  1. Is the racket.Base big enough
  2. Is the racket.Base set to CanCollide = false?
local player = game.Players.LocalPlayer
local Racket = player.Character.Racket
local ball = game.Workspace.ball
local tool = script.Parent
local canshoot = true
--local boomy = script.Parent:WaitForChild("BallBoom")
local event = game.ReplicatedStorage.RacketEvents.RacketClient
local con = nil


local overlapParams = OverlapParams.new()
overlapParams.FilterDescendantsInstances = {ball} -- Detect ball
overlapParams.FilterType = Enum.RaycastFilterType.Whitelist -- Only detect in filter list


		
		
	function OnActivation()
		if con ~= nil then con:Disconnect() end

		con = ball.Touched:connect(function(hit) 


			local parts = workspace:GetPartsInPart(Racket.Base, overlapParams)

			for _, v in ipairs(parts) do
			event:FireServer()
			if canshoot == true then
				canshoot = false
				print('Activation works')
				--event:FireServer(boomy)
				event:FireServer()


				wait(1)
				canshoot = true

			end
			end
	end)
	end


	
tool.Activated:Connect(OnActivation)

I’m not sure where the box is in the script, so I’m assuming it’s the Racket variable you have.
You need to have the Box in FilterDescendantsInstances, then you call GetPartsInPart from the tool part.

Also make sure that the tool part has CanCollide false or else it Roblox won’t be able to tell if it is in a part.

local player = game.Players.LocalPlayer
local Racket = player.Character.Racket
local ball = workspace.ball
local tool = script.Parent
local canshoot = true
--local boomy = script.Parent:WaitForChild("BallBoom")
local event = game.ReplicatedStorage.RacketEvents.RacketClient

local overlapParams = OverlapParams.new()
overlapParams.FilterDescendantsInstances = {ball}
overlapParams.FilterType = Enum.RaycastFilterType.Whitelist

function OnActivation()
	local parts = workspace:GetPartsInPart(tool.Base, overlapParams)
	
	if parts[1] and canshoot then
		canshoot = false
		print("Activation works")
		--event:FireServer(boomy)
		event:FireServer()
		task.wait(1)
		
		canshoot = true
	end
end
	
tool.Activated:Connect(OnActivation)

box is not the racket, its the ball. I know its confusing sorry.

Here is a Roblox place where it works if you have any problems. The tool and script are in StarterPack.
ToolInBox.rbxl (33.2 KB)

I compared the scripts and turns out the disconnecting thing was messing with the script, the one the other guy gave me. Thanks for the help.