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

How do I make a Humanoid Touch function work in LocalScript in PlayerScripts?

Asked by 3 years ago

I have a local script in player scripts which makes parts fall, then respawn after a cool-down.

This script worked fine in starter character scripts except that it wouldn't respawn parts when the player died before it respawned. I figured out this is because it is deleted when the character dies so I put it in starter player scripts.

The problem is that I can't figure out how to get the player's humanoid it needs in order to detect when the player touches a falling part, make it fall, and respawn.

How do I find the humanoid of the player, and how do I make sure it still works when the player respawns with a new character and humanoid?

Here is the entire script in case it helps with the problem:

local character = script.Parent 

local humanoid = character:WaitForChild("Humanoid")

--Tweak these
local delay = 0.5
local respawntime = 5
local appeartime = 0.3

--Maybe tweak these
local falldistance = -10
local acceleration = -30

local TweenService = game:GetService("TweenService")

local time = math.sqrt(2 * falldistance/acceleration)

local triggeredParts = {}

local touchedConnection = humanoid.Touched:Connect(function(Part, limb)
    if Part.Name ~= "FallingPartTest" then
        return
    end

    --if falling part is in list don't run  
    for i, fallingpart in pairs(triggeredParts) do
        if Part == fallingpart then
            return
        end
    end

    local falltweeninfo = TweenInfo.new(
        time, --time for animation
        Enum.EasingStyle.Exponential, --Style of tween
        Enum.EasingDirection.In, --Direction of tween
        0, --Repeat times
        false, --Repeat?
        0 --Delay between repeats
    )

    local transparencytweeninfo = TweenInfo.new(
        time,
        Enum.EasingStyle.Exponential,
        Enum.EasingDirection.In,
        0,
        false,
        0
    )

    local respawntweeninfo = TweenInfo.new(
        appeartime,
        Enum.EasingStyle.Linear,
        Enum.EasingDirection.In,
        0,
        false,
        0
    )

    local TweenPartFall = TweenService:Create(Part, falltweeninfo, {CFrame = Part.CFrame * CFrame.new(0,falldistance,0)})
    local TweenPartTransparency = TweenService:Create(Part, transparencytweeninfo, {Transparency = 1})
    local TweenPartRespawn = TweenService:Create(Part, respawntweeninfo, {Transparency = 0})

    local Origin = Part.CFrame

    --add part to triggered list
    table.insert(triggeredParts, Part)

    wait(delay)
    Part.CanCollide = false
    TweenPartFall:Play()
    TweenPartTransparency:Play()
    wait(respawntime)
    Part.CFrame = Origin
    TweenPartRespawn:Play()
    Part.CanCollide = true

    --find and remove part from triggered list
    for i, fallingpart in pairs(triggeredParts) do
        if Part == fallingpart then
            table.remove(triggeredParts, i)
        end
    end
end)

0
Update: I got it working. Keita2282 2 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Update: I got it working. Please tell me if this script has any problems. I'd like to make my scripts as best I can.

local Players = game:GetService("Players")
local player = Players.LocalPlayer

player.CharacterAdded:Connect(function(character)
    local humanoid = character:WaitForChild("Humanoid")

    --Tweak these
    local delay = 0.5
    local respawntime = 5
    local appeartime = 0.3

    --Maybe tweak these
    local falldistance = -10
    local acceleration = -30

    local TweenService = game:GetService("TweenService")

    local time = math.sqrt(2 * falldistance/acceleration)

    local triggeredParts = {}

    local touchedConnection = humanoid.Touched:Connect(function(Part, limb)
        if Part.Name ~= "FallingPartTest" then
            return
        end

        --if falling part is in list don't run  
        for i, fallingpart in pairs(triggeredParts) do
            if Part == fallingpart then
                return
            end
        end

        local falltweeninfo = TweenInfo.new(
            time, --time for animation
            Enum.EasingStyle.Exponential, --Style of tween
            Enum.EasingDirection.In, --Direction of tween
            0, --Repeat times
            false, --Repeat?
            0 --Delay between repeats
        )

        local transparencytweeninfo = TweenInfo.new(
            time,
            Enum.EasingStyle.Exponential,
            Enum.EasingDirection.In,
            0,
            false,
            0
        )

        local respawntweeninfo = TweenInfo.new(
            appeartime,
            Enum.EasingStyle.Linear,
            Enum.EasingDirection.In,
            0,
            false,
            0
        )

        local TweenPartFall = TweenService:Create(Part, falltweeninfo, {CFrame = Part.CFrame * CFrame.new(0,falldistance,0)})
        local TweenPartTransparency = TweenService:Create(Part, transparencytweeninfo, {Transparency = 1})
        local TweenPartRespawn = TweenService:Create(Part, respawntweeninfo, {Transparency = 0})

        local Origin = Part.CFrame

        --add part to triggered list
        table.insert(triggeredParts, Part)

        wait(delay)
        Part.CanCollide = false
        TweenPartFall:Play()
        TweenPartTransparency:Play()
        wait(respawntime)
        Part.CFrame = Origin
        TweenPartRespawn:Play()
        Part.CanCollide = true

        --find and remove part from triggered list
        for i, fallingpart in pairs(triggeredParts) do
            if Part == fallingpart then
                table.remove(triggeredParts, i)
            end
        end
    end)
end)

Ad

Answer this question