Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to see the object that has hit the part?

Asked by
14dark14 167
4 years ago

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

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
Ad

Answer this question