Why is this script not working properly?

So I’ve been meaning to make a stage selection system. Once you click on a button, you will be sent to the right stage according to your playerstats.
So sometimes, once I click it, it won’t work.
Any fixes for this?

local ObbyFrame = script.Parent.Parent.Parent.Parent
local ObbySelection = script.Parent.Parent.Parent
local Button = script.Parent
local FreeObby = script.Parent.Parent.Parent.Parent.FreeObbies
local PremiumObby = script.Parent.Parent.Parent.Parent.PremiumObbies
local VIP = script.Parent.Parent.Parent.Parent.VIPObbies


local Player = game.Players.LocalPlayer
local Stage = Player:FindFirstChild("leaderstats"):FindFirstChild('Stage').Value
local Char = Player.Character

local TelParts = game.Workspace.TelParts.SpaceObby

Button.Activated:Connect(function()
	ObbyFrame.Visible = false
	ObbySelection.Visible = false
	FreeObby.Visible = true
	PremiumObby.Visible = true
	VIP.Visible = true
	
	if Stage == 61 then
		Char:PivotTo(TelParts.TelPart1.CFrame)
	end
	if Stage == 62 then
		Char:PivotTo(TelParts.TelPart2.CFrame)
	end
	if Stage == 63 then
		Char:PivotTo(TelParts.TelPart3.CFrame)
	end
	if Stage == 64 then
		Char:PivotTo(TelParts.TelPart4.CFrame)
	end
	if Stage == 65 then
		Char:PivotTo(TelParts.TelPart5.CFrame)
	end
	if Stage == 66 then
		Char:PivotTo(TelParts.TelPart6.CFrame)
	end
	if Stage == 67 then
		Char:PivotTo(TelParts.TelPart7.CFrame)
	end
	if Stage == 68 then
		Char:PivotTo(TelParts.TelPart8.CFrame)
	end
	if Stage == 69 then
		Char:PivotTo(TelParts.TelPart9.CFrame)
	end
	if Stage == 70 then
		Char:PivotTo(TelParts.TelPart10.CFrame)
	end
end)

Your gonna want to move the local Stage = Player:FindFirstChild("leaderstats"):FindFirstChild('Stage').Value down after the button is activated. Right now your only getting the stage value when this script first runs. You want to get it whenever the button is clicked.

I would also move local char = Player.Character down after activated depending on where this script lives.

local ObbyFrame = script.Parent.Parent.Parent.Parent
local ObbySelection = script.Parent.Parent.Parent
local Button = script.Parent
local FreeObby = script.Parent.Parent.Parent.Parent.FreeObbies
local PremiumObby = script.Parent.Parent.Parent.Parent.PremiumObbies
local VIP = script.Parent.Parent.Parent.Parent.VIPObbies

local Player = game.Players.LocalPlayer
local TelParts = game.Workspace:WaitForChild("TelParts"):WaitForChild("SpaceObby")

Button.Activated:Connect(function()
	ObbyFrame.Visible = false
	ObbySelection.Visible = false
	FreeObby.Visible = true
	PremiumObby.Visible = true
	VIP.Visible = true

    local Stage = Player:FindFirstChild("leaderstats"):FindFirstChild('Stage').Value
    local Char = Player.Character

    if not Char then
        return
    end

    if not Stage then
        return
    end

    local function pivotTo(cf: CFrame)
        Char:PivotTo(cf * CFrame.new(0, 4, 0))
    end

	if Stage == 61 then
        pivotTo(TelParts.TelPart1.CFrame)
	end
	if Stage == 62 then
        pivotTo(TelParts.TelPart2.CFrame)
	end
	if Stage == 63 then
        pivotTo(TelParts.TelPart3.CFrame)
	end
	if Stage == 64 then
        pivotTo(TelParts.TelPart4.CFrame)
	end
	if Stage == 65 then
        pivotTo(TelParts.TelPart5.CFrame)
	end
	if Stage == 66 then
        pivotTo(TelParts.TelPart6.CFrame)
	end
	if Stage == 67 then
        pivotTo(TelParts.TelPart7.CFrame)
	end
	if Stage == 68 then
        pivotTo(TelParts.TelPart8.CFrame)
	end
	if Stage == 69 then
        pivotTo(TelParts.TelPart9.CFrame)
	end
	if Stage == 70 then
        pivotTo(TelParts.TelPart10.CFrame)
	end
end)

Please change the if’s to something like this:

local teleport_part = TelParts:FindFirstChild("Stage"..tostring(Stage))
if teleport_part then
	Char:PivotTo(telepor_part)
end

If you do it like this you will need to change your teleport parts’ (in game.Workspace.TelParts.SpaceObby) name to Stage(stage number) for example Stage61
This way you can remove all of the if statements and if you need to add a Stage it won’t require you to write it out in another if

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.