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

This script takes 4 seconds to function, and I keep spawning in the sky! How do I fix this?

Asked by 4 years ago

I am making a 2D platformer, and I am having some weird issues. Sometimes, I spawn in the sky. Other times, the "win part" takes 4 seconds to teleport a player. I'm not sure what the issue is.

Humanoid Setup:

game.Players.PlayerAdded:Connect(function(p)
    p.CharacterAdded:Connect(function(c)
        local h = c.Humanoid
        h.WalkSpeed = 32
        h.JumpPower = 100
    end)
end)

Leaderboard:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("platformer-V9")
local loaded= false

game.Players.PlayerAdded:Connect(function(p)
    local ls = Instance.new("Folder")
    ls.Parent = p
    ls.Name = "leaderstats"

    local vcf = Instance.new("IntValue")
    vcf.Parent = ls
    vcf.Name = "Level"
    vcf.Value = 1

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(p.UserId.."-level")
    end)

    if success then
        vcf.Value = data or vcf.Value
        loaded = true
    else
        print("ERROR WHILE GETTING DATA!")
        warn(errormessage)
    end

    repeat wait()

    until p.Character
    local c = p.Character
    if loaded ~= true then
        repeat wait()

        until loaded == true and c:FindFirstChild("HumanoidRootPart")
    end
    wait(2)
    local lvls = {}
    for _,level in pairs(workspace.Levels:GetChildren()) do
        lvls[level:WaitForChild("Stage").Value] = level
    end
    local h = c:WaitForChild("HumanoidRootPart")
    local ttt2 = vcf.Value or 0
    h.CFrame = lvls[ttt2].StartPart.CFrame

end)


game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId.."-level",player.leaderstats.Level.Value)
    end)

    if success then
        print("OMG! IT WAS SAVED!")
    else
        print("NOOOO! THERE WAS AN ERROR!")
        warn(errormessage)
    end
end)

Teleporter (IMPORTANT SCRIPT):

local lf = game.Workspace.Levels
local lvls = {}
local c = false
for i,level in pairs(lf:GetChildren()) do
    local wp = level:WaitForChild("WinPart")
    lvls[i] = level
    wp.Touched:Connect(function(tou)
        if c ~= true then
            if game.Players:GetPlayerFromCharacter(tou.Parent) then
                c = true
                wait(2)
                local tp = game.Players[tou.Parent.Name]
                local h = tp.Character:WaitForChild("HumanoidRootPart")
                tp.leaderstats.Level.Value = tp.leaderstats.Level.Value + 1
                h.CFrame = lvls[tp.leaderstats.Level.Value].StartPart.CFrame
                coroutine.resume(coroutine.create(function()
                    wait(1)
                    c = true
                end))
            end
        end
    end)
end

Camera Script (DOUBT THIS IS THE ISSUE):

local cameraHeight = 1
local cameraZOffset = 35
local cameraXChase = 1
local cameraSpeed = .25

local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

local function setupCamera()
    camera.CFrame = CFrame.new(Vector3.new(0,cameraHeight,cameraZOffset),
                                        Vector3.new(0,cameraHeight,0))
end
setupCamera()
player.CharacterAdded:connect(setupCamera)

local function onUpdate()
    if player.Character and player.Character:FindFirstChild('UpperTorso') then
        camera.CFrame = CFrame.new(player.Character:WaitForChild("HumanoidRootPart").CFrame.p + Vector3.new(0,cameraHeight,cameraZOffset))
    end
end

RunService.Heartbeat:Connect(function()
    onUpdate()
end)

Control Script (DOUBT THIS IS THE ISSUE):

local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')
local ContextActionService = game:GetService('ContextActionService')

local jumping = false
local sliding = false
local leftValue, rightValue = 0, 0

local function onLeft(actionName, inputState)
    if inputState == Enum.UserInputState.Begin then 
        leftValue = 1
    elseif inputState == Enum.UserInputState.End then
        leftValue = 0
    end
end

local function onRight(actionName, inputState)
    if inputState == Enum.UserInputState.Begin then
        rightValue = 1
    elseif inputState == Enum.UserInputState.End then
        rightValue = 0
    end
end

local function onJump(actionName, inputState)
    if inputState == Enum.UserInputState.Begin then
        jumping = true
    elseif inputState == Enum.UserInputState.End then
        jumping = false
    end
end

local function onUpdate()
    if player.Character and player.Character:FindFirstChild('Humanoid') then
        if jumping then
            player.Character.Humanoid.Jump = true
        end
        local moveDirection = rightValue - leftValue
        player.Character.Humanoid:Move(Vector3.new(moveDirection,0,0), false)
    end
end

RunService:BindToRenderStep('Control', Enum.RenderPriority.Input.Value, onUpdate)

ContextActionService:BindAction('Left', onLeft, true, 'a', Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
ContextActionService:BindAction('Right', onRight, true, 'd', Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
ContextActionService:BindAction('Jump', onJump, true, 'w', Enum.KeyCode.Space, Enum.KeyCode.Up, Enum.KeyCode.DPadUp, Enum.KeyCode.ButtonA)

Answer this question