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

how to see the object that has hit the part?

I have a script that will give you a tool if your torso touched the part, but I can't get it to work. Player's arms and legs also activate the script and I just can't fix it!

local part = script.Parent
local Debounces = {}
local function onTouch(part)
	 local Player = game.Players:GetPlayerFromCharacter(part.Parent)
        if Player and not Debounces[Player.UserId] then
	     if part.Parent == Player.Character:FindFirstChild("Torso") then
            Debounces[Player.UserId] = true
print(part)
end
end
end
part.Touched:Connect(onTouch)

1 answer

✓ Accepted answer

In theory for the Player Object initiating contact with an Instance, the part returned by the .Touched signal' parent should refer to the Player Object's Character Rig.

Therefore by calling part.Parent, you're referencing the Character, not the part that initiated contact; you'll be comparing a Model to a BasePart.

local players = game:GetService("Players")

local touchedAlready = {}

local function onTouch(part)
	local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
	if (humanoid) then
		local player = players:GetPlayerFromCharacter(part.Parent)
		if (player and not touchedAlready[player]) then
			if (part == part.Parent.Torso) then
				table.insert(touchedAlready, player)
			end
		end
	end
end

script.Parent.Touched:Connect(onTouch)

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