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

TouchEnded firing while still touching a part?

Asked by 5 years ago

I have a script inside of an invisible part that detects when a player is inside of it and sets a BodyForce in the Torso of the player from (0, 0, 0) to (0, -200, 0). This script works alongside another that sets the NumberValues named "Heat" and "Oxygen" inside of the player to 65 and 100. When you enter the part the Touched event fires as normal but after doing so much as jumping, the TouchEnded event fires and resets the values. I have tested the scripts inside of a very large part that will always be touching the player at any given time and the problem still occurs.

Here are the two scripts:

The Heat script

local part = script.Parent


local debounce = false

part.Touched:Connect(function(hit)
    if debounce == false then
        debounce = true

        local hum = hit.Parent:FindFirstChild('Humanoid')

        if hum and hit.Name == "UpperTorso" then
            if hum.Health > 0 then

                local Oxygen = game.Players:GetPlayerFromCharacter(hit.Parent):WaitForChild("Oxygen")
                local Heat = game.Players:GetPlayerFromCharacter(hit.Parent):WaitForChild("Heat")

                if Heat and Oxygen then
                    Heat.Value = 65
                    Oxygen.Value = 100
                end
            end
        end
        debounce = false
    end
end)


part.TouchEnded:Connect(function(hit)
    if debounce == false then
        debounce = true

        local hum = hit.Parent:FindFirstChild('Humanoid')

        if hum and hit.Name == "UpperTorso" then
            if hum.Health > 0 then

                local Oxygen = game.Players:GetPlayerFromCharacter(hit.Parent):WaitForChild("Oxygen")
                local Heat = game.Players:GetPlayerFromCharacter(hit.Parent):WaitForChild("Heat")

                if Heat and Oxygen then
                    Heat.Value = -455
                    Oxygen.Value = 0
                end
            end
        end
        debounce = false
    end
end)


and the Gravity script

local part = script.Parent
local debounce = false

part.Touched:Connect(function(hit)
    if debounce == false then
        debounce = true

        local hum = hit.Parent:FindFirstChild('Humanoid')

        if hum and hit.Name == "UpperTorso" then
            if hum.Health > 0 then

                local Gravity = hit.Parent.UpperTorso:FindFirstChild("GravityForce").Force
                if Gravity then
                    if Gravity ~= Vector3.new(0, -200, 0) then
                         Gravity = Vector3.new(0, -200, 0) 
                    end
                end
            end
        end

        debounce = false
    end
end)



part.TouchEnded:Connect(function(hit)
    if debounce == false then
        debounce = true

        local hum = hit.Parent:FindFirstChild('Humanoid')

        if hum and hit.Name == "UpperTorso" then

            if hum.Health > 0 then
                local Gravity = hit.Parent.UpperTorso:FindFirstChild("GravityForce").Force
                if Gravity then
                    if Gravity ~= Vector3.new(0, 0, 0) then
                         Gravity = Vector3.new(0, 0, 0)
                    end
                end
            end
        end

        debounce = false
    end
end)

0
TouchEnded will fire even if the player is remaining still on a given part, because of roblox idle animations. It's not reliable in most cases Gey4Jesus69 2705 — 5y
0
is there another method of resetting when the player exits the part? Touched works fine for my case. XXXlawsonXXX 52 — 5y

Answer this question