#How do I fix my falling platform script?

34 messages · Page 1 of 1 (latest)

shy oxide
#

I'm trying to make a local script in StarterPlayerScripts where when you touch a tagged object it falls. The problem I'm having is that when one platform is falling I have to wait for it to finish before the next platform falls. I assume its something to do with the debounce but I don't know how I would change that.

script:

local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")
local localplayer = game.Players.LocalPlayer
local db = false

for _, platform in pairs(CollectionService:GetTagged("Fall")) do
    local part = platform:WaitForChild("Platform")
    local partpos = part.Position
    part.Touched:connect(function(hit)
        if db == false and hit.Parent.Name == localplayer.Name then
            db = true
            local partclone = part:Clone()
            part:Clone()
            partclone.Parent = game.Workspace
            partclone.Transparency = 1
            partclone.CanCollide = false
            local MoveCFrame = part.CFrame * CFrame.new((0), (-1), (0))
            local tweenInfo = TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,true,0)
            local Move = TweenService:Create(part, tweenInfo,{CFrame = MoveCFrame})
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
            if(hit.Parent:FindFirstChild("Humanoid")~=nil)then
                Move:Play()
                Move.Completed:Wait()
                partclone.Transparency = 0
                partclone.Anchored = false
                part.Transparency = 1
                part.CanCollide = false
                part.CanTouch = false
                task.wait(2)
                partclone:Destroy()
                part.Transparency = 0
                part.CanCollide = true
                part.CanTouch = true
                db = false
            end
        end
    end)
end
cunning harness
#

thats some very bad code

shy oxide
#

I couldn't paste it in correctly for some reason, also I'm not that experienced with scripting

cunning harness
#

what object are you giving the tag "Fall"

#

or could you show me ur explorer maybe

shy oxide
#

its a model

cunning harness
#

alright

#

well it works for me

#

kinda

shy oxide
#

yeah it works but I can't have multiple fall at the same time

cunning harness
#

lemme try

#
local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")
local localplayer = game.Players.LocalPlayer

for _, platform in pairs(CollectionService:GetTagged("Fall")) do
    local part = platform:WaitForChild("Platform")
    local partpos = part.Position
    local db = false
    part.Touched:connect(function(hit)
        if db == false and hit.Parent.Name == localplayer.Name then
            db = true
            local partclone = part:Clone()
            part:Clone()
            partclone.Parent = game.Workspace
            partclone.Transparency = 1
            partclone.CanCollide = false
            local MoveCFrame = part.CFrame * CFrame.new((0), (-1), (0))
            local tweenInfo = TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,true,0)
            local Move = TweenService:Create(part, tweenInfo,{CFrame = MoveCFrame})
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
            if(hit.Parent:FindFirstChild("Humanoid")~=nil)then
                Move:Play()
                Move.Completed:Wait()
                partclone.Transparency = 0
                partclone.Anchored = false
                part.Transparency = 1
                part.CanCollide = false
                part.CanTouch = false
                task.wait(2)
                partclone:Destroy()
                part.Transparency = 0
                part.CanCollide = true
                part.CanTouch = true
                db = false
            end
        end
    end)
end

you used the same debounce for all plates this should fix it

shy oxide
#

tysm

cunning harness
#

np

#

holy i just realized how bad this code is

#

why are you cloning

shy oxide
#

idk 💀

#

I'm new so idk stuff

coarse jettyBOT
#

studio** You are now Level 1! **studio

cunning harness
#

how is it going down i dont get it

#

ohh

#

I get it

shy oxide
#

what can I do to make the code better

cunning harness
#

gimme a second

#
local CollectionService = game:GetService("CollectionService") --Getting CollectionService
local TweenService = game:GetService("TweenService") --Getting TweenService

for _, model in CollectionService:GetTagged("Fall") do --Looping through tagged models
    local part = model:WaitForChild("Platform") --Getting the platform
    local partPos = part.Position --Getting the position of the platform
    local db = false --Setting debounce to false
    
    part.Touched:connect(function(hit) --Detecting a touch
        if db == true or not hit.Parent.Humanoid then --Checking if debounce is true or if the hit object is not a player
            return
        end
        db = true --Setting debounce to true
        
        local newPos = partPos - Vector3.new(0, 1, 0) --Creating a new position for the platform to move to
        local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,true,0) --Creating a tween info
        local wiggleAnimation = TweenService:Create(part, tweenInfo,{Position = newPos}) --Creating a tween
        
        wiggleAnimation:Play() --Playing the tween
        wiggleAnimation.Completed:Wait() --Waiting for the tween to finish
        
        part.Anchored = false 
        part.CanCollide = false
        
        task.wait(2) --Waiting 2 seconds
        
        part.Anchored = true
        part.Position = partPos
        
        db = false --Setting debounce to false
    end)
end

@shy oxide

#

you see how much easier my script is to read?

#

also for changing position u dont need to use cframe

#

if u got any questions js ask me

shy oxide
#

oh okay, ty

worthy zenith
cunning harness
#

leave my color theme alone >:(

worthy zenith
#

why tf are your comments black 😭