Touched Event Not Working

Hello everyone!

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a magnet that pulls in coins in its radius like Magnet Simulator 1 & 2

  2. What is the issue? Include screenshots / videos if possible!
    Im sending a remoteEvent to the server when a magnet tool is equipped and thats where i make a hitbox but the touch event for the hitbox doesnt work.
    image

Client:

local char = script.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local rss = game:GetService("ReplicatedStorage")
local magnetEvent = rss.Events.Magnet.Magnet

local tool = plr.Backpack:FindFirstChildOfClass("Tool")
if tool:GetAttribute("Magnet") then
	tool.Equipped:Connect(function(mouse)
		magnetEvent:FireServer(tool,false)
	end)
	tool.Unequipped:Connect(function()
		magnetEvent:FireServer(tool,true)
	end)
end

Server:

local hb = nil

game:GetService("ReplicatedStorage").Events.Magnet.Magnet.OnServerEvent:Connect(function(plr,tool,IsUnequipping)
	local char = plr.Character
	if IsUnequipping == false then
		hb = Instance.new("Part")
		hb.Shape = Enum.PartType.Cylinder
		hb.Name = "hitbox-"..plr.Name
		hb.Parent = workspace.Map.RangeParts
		hb.Size = Vector3.new(20,tool:GetAttribute("Range")*2,tool:GetAttribute("Range")*2)
		hb.Rotation = Vector3.new(0,0,-90)
		hb.Position = char.HumanoidRootPart.Position
		hb.Anchored = true
		hb.CanCollide = false
		hb.Transparency = 0.8
		while task.wait(0.1) do
			workspace.Map.RangeParts:WaitForChild(hb.Name).Position = char.HumanoidRootPart.Position
		end
		hb.Touched:Connect(function(hit)
			if not hit.Parent.Humanoid then
				if hit:GetAttribute("Coin") == true then
					print("is coin")
				end
			end
		end)
	else
		hb:Destroy()
		hb = nil
	end
end)

The output only gives one error even though said error doesn’t effect the magnet range part at all. It still works as intended
Error:

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes, but none were specific to my situation.

Is the client script in StarterCharacterScripts or in the tool? I need to know this so i can see what the problem is

StarterCharacterScripts because char = script.Parent

Why are you checking for an attribute instead of just changing the name of the tool to Magnet and using

FindFirstChild()

?

nvm

I guess attributes was the first thing i thought of

instead of this do

	while hb do
		task.wait()
		hb.Position = char.HumanoidRootPart.Position
	end

Sorry for the late reply but try replacing your server script with this:

local hb = nil

game:GetService("ReplicatedStorage").Events.Magnet.Magnet.OnServerEvent:Connect(function(plr,tool,IsUnequipping)
	local char = plr.Character
	if IsUnequipping == false then
		hb = Instance.new("Part")
		hb.Shape = Enum.PartType.Cylinder
		hb.Name = "hitbox-"..plr.Name
		hb.Parent = workspace.Map.RangeParts
		hb.Size = Vector3.new(20,tool:GetAttribute("Range")*2,tool:GetAttribute("Range")*2)
		hb.Rotation = Vector3.new(0,0,-90)
		hb.Position = char.HumanoidRootPart.Position
		hb.Anchored = true
		hb.CanCollide = false
		hb.Transparency = 0.8
		while task.wait(0.1) do
			workspace.Map.RangeParts:WaitForChild(hb.Name).Position = char.HumanoidRootPart.Position
		end
		hb.Touched:Connect(function(hit)
			if not hit.Parent:FindFirstChild("Humanoid") then
				if hit:GetAttribute("Coin") == true then
					print("is coin")
				end
			end
		end)
	else
		hb:Destroy()
		hb = nil
	end
end)

Sorry for the late reply aswell. Since I thought about the lag it might cause due to this being a tool I decided to rework the script to include the hitbox on the client and only to invoke the server when it gets touched to add to coins leaderstat. This code should still help me figure out whatever the hell im doing.

I personally would do this a different way. Consider using either workspace:GetPartBoundsInRadius() or workspace:GetPartBoundsInBox() (documentation is under WorldRoot [WorldRoot | Documentation - Roblox Creator Hub]). I believe this could be better for your situation. If you need a better explanation I’d be happy to provide it.

That makes the game vulnerable as exploiters can just use FireServer and get free coins, unless you do checks on the server
For the server script put the while loop after the Touched connection, else it won’t work as while loops make the code yield and won’t run any code behind it until the loop’s broken or stopped

1 Like

This also silently answers my “WHAT IS CANQUERY” question that I’ve had in my head forever. I will most likely use this, thanks!

Well that actually solves it. I wish i could mark both of you as the solution but thank you both!

1 Like

You have two options to make it secure. you can either handle it on the client. if you choose to handle it on the client make the checks on the server semi to really strict (also make sure to typecheck the arguments if they are an Instances or not).

I have decided to go on server
guess what: NEW ERROR!
when i say if not hit.Parent.Humanoid Luau is doing is usual thing and completely ignoring that if statement and complaining that Instance.Humanoid does not exist

Edit: Forgot that im stupid and did not change the code to what you suggested

Im trying to use the OverlapParams for collision groups, but i have no idea how to write them ;-;.
also if use this do i have to use the hitbox?

edit: and i solved it again, hopefully this is the last time you get a notification from me :skull:

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