Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a part only print through a remote event when clicked?

I am trying to make a sword and am trying to make it detect when ever it touches a part. It works but I am trying to make it only happen when I click and so far, after I click it once, it will always print. Here is local script for the tool.

local mouse = game.Players.LocalPlayer:GetMouse()
local db = true
script.Parent.Equipped:Connect(function()
	mouse.Button1Down:Connect(function()
	if db == true then
		script.Parent.sword_event:FireServer()
		db = false
		wait(0.1)
		db = true
	end
end)

end)



I don't think there is any problems with it and I think my server script is causing all the problems

local event = script.Parent.OnServerEvent:Connect(function(player)
	local tool = game.Workspace[player.Name].Tool
	tool.Handle.Touched:Connect(function(part)
		if part.Parent == game.Workspace[player.Name] then
			print("e")
		else
			print("r")
			
		end
	
	end)	
end)

Please help me!

1 answer

✓ Accepted answer

Since you're using a tool, you don't need a LocalScript for touched events. Tools work with both Scripts and LocalScripts.

ServerScript inside Tool:

local Tool = script.Parent
local Handle = Tool:WaitForChild('Handle')
local char,plr

Tool.Equipped:Connect(function()
	if not char or not plr then
		char = Tool.Parent
		plr = game:GetService('Players'):GetPlayerFromCharacter(char)
	end
end)

local db = false
Handle.Touched:Connect(function(hit)
	if hit and hit.Parent:IsA('Model') and hit.Parent ~= char and hit.Parent:FindFirstChild('Humanoid') and not db then
		db = true
			
		print('This is another player')

		wait(.1)
		db = false
	end
end)

Also, Tools already have an event for checking when the mouse button was pressed. An example of that would be:

Tool.Activated:Connect(function()
	print('mouse button 1 was pressed!')
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.