Why does function runs only after player dies?

This is local scripts, must runs when player join the game, But it’s runs only after player dies.


local function OnChanged()

	local stageNumber = tonumber(TeleStage.Value)

	local NotReallyStage = tonumber(Stage.Value)

	for _, v in pairs(folder:GetChildren()) do

		local checkpointNumber = tonumber(v.Name)

		if v:FindFirstChildWhichIsA("Highlight") then

			v:FindFirstChildWhichIsA("Highlight"):Destroy()

		end

		if NotReallyStage > checkpointNumber  then

			v.Color = Color3.fromHex("#57ff79")

			v.Material = Enum.Material.Neon

		end

		if stageNumber == checkpointNumber then

			local tween = TweenService:Create(v, tweenInfo, {Color = colorSequence,})

			v.Material = Enum.Material.Neon

			tween:Play()
		end

		if checkpointNumber == stageNumber + 1 then

			local Highlight = Instance.new("Highlight")
			Highlight.FillColor = Color3.fromRGB(0, 255, 0)
			Highlight.FillTransparency = .9
			Highlight.Parent = v
			Highlight.Enabled = true

			lastKeyPressTime = tick()

		end


	end
end

local function OnPlayerAdded(plr)
	plr.CharacterAppearanceLoaded:Connect(OnChanged)
	-- Connect CharacterAdded event inside this function
	plr.CharacterAdded:Connect(OnChanged)
end

-- Connect OnPlayerAdded to PlayerAdded event
game.Players.PlayerAdded:Connect(OnPlayerAdded)

-- Connect TeleStage's Changed event to OnChanged
TeleStage.Changed:Connect(OnChanged)

OnChanged()

local function OnPlayerAdded(plr)
	plr.CharacterAppearanceLoaded:Connect(OnChanged)
	-- Connect CharacterAdded event inside this function
	plr.CharacterAdded:Connect(OnChanged)
end

because CharacterAdded is triggered everytime when the character gets added to the workspace and imagine like the character gets deleted for a split second and readded again when you respawn

1 Like

What I have to do? adwadwadawwadwadawdawd

Try this


local function OnChanged()

	local stageNumber = tonumber(TeleStage.Value)

	local NotReallyStage = tonumber(Stage.Value)

	for _, v in pairs(folder:GetChildren()) do

		local checkpointNumber = tonumber(v.Name)

		if v:FindFirstChildWhichIsA("Highlight") then

			v:FindFirstChildWhichIsA("Highlight"):Destroy()

		end

		if NotReallyStage > checkpointNumber  then

			v.Color = Color3.fromHex("#57ff79")

			v.Material = Enum.Material.Neon

		end

		if stageNumber == checkpointNumber then

			local tween = TweenService:Create(v, tweenInfo, {Color = colorSequence,})

			v.Material = Enum.Material.Neon

			tween:Play()
		end

		if checkpointNumber == stageNumber + 1 then

			local Highlight = Instance.new("Highlight")
			Highlight.FillColor = Color3.fromRGB(0, 255, 0)
			Highlight.FillTransparency = .9
			Highlight.Parent = v
			Highlight.Enabled = true

			lastKeyPressTime = tick()

		end


	end
end

local function OnPlayerAdded(plr)
	plr.CharacterAppearanceLoaded:Connect(OnChanged)
	-- Connect CharacterAdded event inside this function
	plr.CharacterAdded:Connect(OnChanged)
	if plr.Character then
		OnChanged(plr.Character) 
	end
end

-- Connect OnPlayerAdded to PlayerAdded event
game.Players.PlayerAdded:Connect(OnPlayerAdded)

for i,v in pairs(game.Players:GetChildren()) do
	OnPlayerAdded(v) 
	
end
-- Connect TeleStage's Changed event to OnChanged
TeleStage.Changed:Connect(OnChanged)

OnChanged()

But it’s local script, it doesn’t make sense.

Can you send your entire script

Try doing this, it will detect player’s character and activate the function if the player’s character was added before it is set up, sometimes the character was added earlier than the code

local function OnPlayerAdded(plr)
	plr.CharacterAppearanceLoaded:Connect(OnChanged)
	-- Connect CharacterAdded event inside this function
	if plr.Character then OnChanged() end
	plr.CharacterAdded:Connect(OnChanged)
end
1 Like

Hiya, if you use the CharacterAdded rbxconnection after you’re spawned, it takes you a death for it to register as its checking for if the character is added and not if the character is already spawned. ps. CharacterApparanceLoaded can only be called from the server (iirc).
Instead, wait for the character to load (however you want to do that repeat until, etc.) then run the OnChanged function, then add a connection to CharacterAdded.

“Happy Scripting!”
~ Sincerely, Ihaveash0rtname

1 Like

Also I recommend that you have 1 highlight and just set the adornee of the highlight to the new checkpoint!

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