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

Script doesn't detect the right player?

Asked by 4 years ago
Edited 4 years ago

So what I'm trying to do is to make a function that only happens when the enemy player is touching the stunned player.

Unfortunately my method of detecting it doesn't work for some reason: (char is the stunned player's character and player is the enemy player)

local touchingChar = false
for _, bodyPart in pairs(char:GetChildren()) do
    if bodyPart:IsA("BasePart") then
        for _, part in pairs(bodyPart:GetTouchingParts()) do
            local hum = part.Parent:FindFirstChild("Humanoid")
            if hum and Players:GetPlayerFromCharacter(part.Parent) == player then
                touchingChar = true
                break
            end
        end
    end
end
if not touchingChar then return end

Please help!

EDIT: This function only fires when the enemy player presses a key, so I need to know if he's touching the stunned player at that very moment.

0
again, .Touched function? zadobyte 692 — 4y
0
I don't really understand your question. Can I get a little more information and what you actually need? rabbi99 714 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
local touchingChar do
local EventConnections={}
function touchingChar(char0,char1)
local success,touchingParts=false,{}
    for _,v0 in ipairs(char0:GetChildren())do
        if v0:IsA"BasePart" then
        for _,v1 in ipairs(char1:GetChildren())do
            if v1:IsA"BasePart"and (v0.Position-v1.Position).magnitude<1.5 then success=true;touchingParts[#touchingParts+1]=v1 end
        end
        end
        end
        return success,#touchingParts>0 and touchingParts or nil
        end
end


--[[ 
local success,touchingParts=touchingChar(char0,char1)
if success then
for _,v in ipairs(touchingParts)do print(v.Name)end
end 
]]
Ad

Answer this question