Stage Selector stops working after Death

  1. What do you want to achieve? I want to make a stage selector that works before and after Death.

  2. What is the issue?
    robloxapp-20230226-2153003.wmv (896.0 KB)

  3. What solutions have you tried so far? I have tried everything that I could find on DevForum but still, no progress.

Here are the scripts:
Left Button - Local Script:

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

repeat wait() until plr.Character
local char = plr.Character

local left = script.Parent
local text = script.Parent.Parent.CheckpointsLabel
local checkpoints = game.Workspace.Checkpoints

left.MouseButton1Click:Connect(function()
	local stage = game.Players.LocalPlayer.leaderstats.Stage

	if tonumber(text.Text) == 0 then
		text.Text = stage.Value + 1
	end
	text.Text = tostring(tonumber(text.Text) - 1)
	char:PivotTo(checkpoints[text.Text].CFrame + Vector3.new(0, 5, 0))
end) 

Right Button - Local Script:

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

repeat wait() until plr.Character

local char = plr.Character

local right = script.Parent

local text = script.Parent.Parent.CheckpointsLabel

local checkpoints = game.Workspace.Checkpoints

right.MouseButton1Click:Connect(function()

local stage = game.Players.LocalPlayer.leaderstats.Stage

if tonumber(text.Text) == stage.Value then

text.Text = -1

end

text.Text = tostring(tonumber(text.Text) + 1)

char:PivotTo(checkpoints[text.Text].CFrame + Vector3.new(0, 5, 0))

end)
1 Like

The issue you are having is because you are setting your local char object to the value of the character at the beginning of when that script starts running. On death that character is destroyed and a new one is created. You might need to add an CharacterAdded function in that script to reset the value of your char variable when the new character is created.

Where do I add the “CharacterAdded” function?

1 Like

You should be able to add it after your mouseclick functions. Something else you might be able to try(haven’t tried it myself) is in your mouseclick functions if the char value is nil you could do the code you have above to repeat until and set the char inside there, but you’d have to try that method.

1 Like

I have tried what you said and still nothing. -edit: Nevermind, it works I just didn´t read your comment correctly :sweat_smile:… But thanks for your help, I´m really grateful :smile:.

One more question, why the repeat until though? Can you explain :thinking:?

Sometimes, like after death, there is a period when you have a player but it takes time for it to recreate the character object. So the repeat until is used to wait in the code until the character is recreated and you can access it again. You could see what I mean by opening up your character model in the workspace while running your game and resetting your character. You will see that it gets recreated but it takes time.

Thank you for explaining, I appreciate it :grin:!

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