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

While is touching function? Help!

Asked by 6 years ago
Edited 6 years ago

Hello guys, I'm having a hard time figuring out how to do a IsTouching function for my script to work, what I want to do is that if the player is in a determined area, can press a button which will animate the player through a parkour vault and then move to the other side of the brick, everything works but not very well. I tried with Touched and TouchEnded, but each ticks they are both called and this result in the script to work only sometimes.

This is the current code:

brick.Touched:connect(function(otherPart)
    local char = otherPart.Parent
    local hum = char:FindFirstChild("Humanoid")
    if hum then
        local player = game:GetService("Players"):GetPlayerFromCharacter(char)
        local flags = player:FindFirstChild("Flags")            
        if flags:FindFirstChild("CanKong").Value == false then
            flags:FindFirstChild("CanKong").Value = true
            flags:FindFirstChild("KongInstance").Value = script.Parent.EndPoint
            print("Can kong");
        end
    end
end)

brick.TouchEnded:connect(function(otherPart)
    local char = otherPart.Parent
    local hum = char:FindFirstChild("Humanoid")
    if hum then
        local player = game:GetService("Players"):GetPlayerFromCharacter(char)
        local flags = player:FindFirstChild("Flags")            
        if flags:FindFirstChild("CanKong").Value == true then
            flags:FindFirstChild("CanKong").Value = false
            flags:FindFirstChild("KongInstance").Value = nil
            print("Cannot kong");
        end
    end
end)

I want it to work every time I press the key, and not sometimes!

0
try remove endpoint darkzerobits 92 — 6y
0
the endpoint is not related to the touched event, it's just the brick where the animation should finish... alessandro112 161 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Use a larger invisible part that covers the area around the brick and use it as a detector.

I was talking about something like this:

https://imgur.com/a/jE6OY

Also, here is a code that creates an event that fires only when the entire character entered or exited the area:

local T = {}
local EvObj = Instance.new("BindableEvent")
script.Parent.Touched:Connect(function(p)
    local Ply = game.Players:GetPlayerFromCharacter(p.Parent)
    if Ply then
        if T[Ply.Name] then
            T[Ply.Name][p.Name]=true
        else
            T[Ply.Name]={[p.Name]=true}
            EvObj:Fire(true,Ply)
        end
    end
end)
script.Parent.TouchEnded:Connect(function(p)
    local Ply = game.Players:GetPlayerFromCharacter(p.Parent)
    if Ply then
        if T[Ply.Name] then
            T[Ply.Name][p.Name]=nil
            if #T[Ply.Name]==0 then
                EvObj:Fire(false,Ply)
                T[Ply.Name]=nil
            end
        end
    end
end)

local TouchEvent = EvObj.Event
TouchEvent:Connect(function(e,p)
    print(e,p)
end)
0
I did it like this, the problem is that TouchEnded calls at every tick and so my function returns true alessandro112 161 — 6y
1
I'm gonna check when I come home nooneisback 81 — 6y
0
I edited my post nooneisback 81 — 6y
Ad

Answer this question