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

Why isn't this Skip Stage button teleporting me to the next Checkpoint? What's wrong?

Asked by
Soban06 410 Moderation Voter
4 years ago
Edited 4 years ago

I am making a Skip Stage for my Obby and I have a Skip Stage feature. I have a screen GUI --> TextButton --> Local Script.

I have named the checkpoints as for example, the first checkpoint will be called 1, the next checkpoint 2 and so on.

I do have a touch handler script in the Checkpoints folder which runs when a checkpoint is touched. It is as follows:

local Checkpoints = script.Parent

for i,v in pairs(Checkpoints:GetChildren()) do
    if v:IsA("BasePart") then
        v.Touched:Connect(function(hit)

            if hit.Parent:FindFirstChild("Humanoid") then

                local player = game.Players:GetPlayerFromCharacter(hit.Parent)
                if player then
                    if player.leaderstats.Checkpoint.Value < tonumber(v.Name) then
                        player.leaderstats.Checkpoint.Value = tonumber(v.Name)
                    end
                end
            end
        end)
    end
end

The Local Script which is inside the text button (which should teleport me to the next Checkpoint) is causing out an error. It increases my leaderstats.Checkpoint's Value but just doesn't ****Teleport me****.

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats"):FindFirstChild("Checkpoint") 
local hasLimits = true
local Limits = 3

script.Parent.MouseButton1Click:connect(function()
    if hasLimits == true then
        if Limits ~= 0 then
            Limits = Limits - 1
            script.Parent.Text = "Skip Stage ("..Limits..")"
            leaderstats.Value = leaderstats.Value + 1
            if player ~= nil then
                if player.Character ~= nil then
                    if player.Character:FindFirstChild("Humanoid").Health > 0 then
                        player.Character.HumanoidRootPart.CFrame = game.Workspace:FindFirstChild(leaderstats.Value).CFrame + Vector3.new(0,3,0)
                    end
                end
            end
        end
    else
        if player ~= nil then
            if player.Character ~= nil then
                if player.Character:FindFirstChild("Humanoid").Health > 0 then
                    player.Character.HumanoidRootPart.CFrame = game.Workspace:FindFirstChild(leaderstats.Value).CFrame + Vector3.new(0,3,0)
                end
            end
        end
    end
end)

And in the output is says "Players.Soban06.PlayerGui.ScreenGui.TextButton.LocalScript:15: attempt to index nil with 'CFrame'".

Please tell me how to fix this.

Thanks

0
If Checkpoints is a Folder, or stored as some sort od descendant of workspace, you may have forgotten to dive into said folder as you’re looking for workspace.3 (example checkpoint). Also try, tostring(leaderstats.Value) Ziffixture 6913 — 4y
0
Thanks. It worked. But there is one problem. If I am on my last level and I press the skip stage button, it still increases the value on my leader board even though I don't have those many stages. How can I stop the leader board values from increasing if I am on the last level? Soban06 410 — 4y

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

There is a certain behaviour to :FindFirstChild(). Instead of tossing an error if the return result doesn’t show up, it will simply give us nil without interrupting the Lua thread. This we can use to our advantage if we simply rearrange some code.

By attempting to search for the next Stage through adding one number to the current Checkpoint, if the scenario of which there is no Stage past the Stage we’re at, we will get a nil result. We can simply create a conditional to only move to the next Stage and set the affiliated Checkpoint if this result does not occur. Otherwise, we simply wont do anything.

local Player = game:GetService("Players").LocalPlayer
local Leaderstats = Player:WaitForChild("leaderstats")
local Checkpoint = Leaderstats:WaitForChild("Checkpoint")

local Checkpoints = workspace:WaitForChild("Checkpoints")

local LimitedUsage  = true
local Limits = 3

local Button = script.Parent

Button.MouseButton1Click:Connect(function()
    local TotalLimit = (LimitedUsage and Limits or 1)
    if (TotalLimit <= 0) then Button.Text = "No Skips Left!" return end
    local Character = Player.Character or Player.CharacterAdded:Wait()
    local SetCheckpoint = Checkpoints:FindFirstChild(tostring(Checkpoint.Value + 1))
    if (SetCheckpoint) then
        Character:MoveTo(SetCheckpoint.CFrame.p + Vector3.new(0,3,0))
        Checkpoint.Value = tonumber(SetCheckpoint.Name)
        if (LimitedUsage) then
            Limits = (Limits - 1)
        end
        Button.Text = (LimitedUsage and "Skip Stage ("..Limits..')' or "Skip Stage")
    end
end)

Ad

Answer this question