How can I change the skybox when the player walks on a part in my game?

I’m creating a large obstacle course with multiple stages with different themes. Is it possible to make the skybox change for the player when they step on a checkpoint? And if so, how do I do this?

1 Like

Yes, just use a .Touched Event, get the player from game.Players:GetPlayerFromCharacter(the person who hit it) and fire a remote event to change the skybox locally for that player

You can Just Put all Checkpoints Inside a Folder.
Now Since you Know Where you Stored the checkpoints,

Insert a Skybox Inside each Checkpoint.
This Skybox will be cloned to the Lighting when the Checkpoint is touched.

local CheckPointsFolder =  -- Define the Folder you Stored them In
local Lighting = game:GetService("Lighting")

local touchDebounce = false

local Player = game.Players.LocalPlayer
local Character = Player.Character

for _, checkpoint in pairs(CheckPointsFolder:GetChildren()) do -- Go Through All Checkpoints
	checkpoint.CanTouch = true -- Ensure it is Touchable
	checkpoint.Touched:Connect(function(Hit) -- On Touch
		if Hit.Parent == Character and touchDebounce == false then -- If Character Touched It
			local foundSkybox -- Blank Variable
			for i, child in pairs(checkpoint:GetChildren()) do -- Loop through Checkpoint
				if child:IsA("Sky") then -- If The child is A Sky
					foundSkybox = child -- We Set the Blank variable as the Sky
					break -- Break the Loop Because we Found what we Needed
				end
			end
			if foundSkybox == nil then -- If There is NOT a sky Inside Checkpoint
				warn("Did Not find A Skybox inside " .. checkpoint.Name) -- Warn Error
			else -- or
				if not Lighting:FindFirstChild(foundSkybox.Name) then -- If we Dont find it Already in Lighting
					touchDebounce = true -- Debounce to true
					for o,oldSky in pairs(Lighting:GetChildren()) do -- Loop through Lighting
						if oldSky:IsA('Sky') then -- If it is a sky
							oldSky:Destroy() -- We Destroy it since its the Old Sky, we dont want it
						end
					end
					foundSkybox:Clone().Parent = Lighting -- Clone the new Sky to Lighting
					wait(2) -- After 2 Seconds
					touchDebounce = false -- Debounce to false
				end
			end
		end
	end)
end

(This should be A LocalScript in StarterPlayer → StarterCharacterScripts)

2 Likes

Doesn’t seem to work for me. There’s an error in line 2 saying: Syntax error: Expected identifier when parsing expression, got ‘local’
local error

Above the error there is Local CheckPointsFolder and an = sign and nothing next to it.
that’s the error, so to get rid of it define the checkpoints folder next to it

1 Like

That error is gone, but it still doesn’t do anything, and now this error in output appears
line 9 error

I Tested it out in studio and for me it turned out perfectly.

I think you Did not Define The First Variable Correctly.

Make A Folder In Workspace
Rename It to CheckpointStorage
Move ALL Your Checkpoints Inside it.

Now On the First Line Saying

local CheckPointsFolder =  -- Define the Folder you Stored them In

Type

local CheckPointsFolder = game.Workspace.CheckpointStorage

Make Sure you have A “Sky” Inside each of your Checkpoints,
Then test it out!