Oh, thank you so much, this worked perfectly! But sorry, one more question, I hope I’m not bothering you… I’d like to know, how can I make it so that the last saved position is stored when the player touched the checkpoint and respawn at the last checkpoint touched?
-- Coroutine function to enable/disable visual effects (VFX) for a specified duration
local function enableVFXForDuration(arcsVFX, duration)
if arcsVFX then
arcsVFX.Enabled = true
wait(duration)
arcsVFX.Enabled = false
else
warn("ArcsVFX not found!")
end
end
-- Function to change the colors of checkpoint parts
local function changeColors(checkpointParts, partColors)
for i, part in ipairs(checkpointParts) do
part.Color = partColors[i]
end
end
-- Coroutine function to play animation from a humanoid for a specified duration
local function playAnimationFromHumanoid(humanoid, animationId, duration)
if animationId then
local animationTrack = Instance.new("Animation")
animationTrack.AnimationId = animationId
local animation = humanoid:LoadAnimation(animationTrack)
if animation then
animation:Play()
wait(duration)
animation:Stop()
else
warn("Failed to load animation - sanitized ID:", animationId)
end
else
warn("Animation ID not provided.")
end
end
-- Coroutine function to play a sound for a specified duration
local function playSound(soundId, duration)
if soundId then
local sound = Instance.new("Sound")
sound.SoundId = soundId
sound.Parent = game.Workspace
sound:Play()
wait(duration)
sound:Destroy()
else
warn("Sound ID not provided.")
end
end
-- Functionm to get the saved player position
local function getPlayerPosition(player)
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerPositions")
return playerDataStore:GetAsync(player.UserId) or nil
end
-- Function to save the player's position to DataStore
local function savePlayerPosition(player)
-- Check if the player has a character
local character = player.Character
if not character then
return nil
end
-- Get the player's position as a string
local playerPosition = tostring(character.HumanoidRootPart.Position)
-- Store player's position in DataStore
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerPositions")
local success, errorMessage = pcall(function()
playerDataStore:SetAsync(player.UserId, playerPosition)
end)
if not success then
warn("Failed to save player position to DataStore:", errorMessage)
return nil
end
return playerPosition -- Return the saved position
end
-- Function to handle the player's respawn
local function onPlayerRespawn(character)
-- Get the player's saved position from DataStore
local player = game.Players:GetPlayerFromCharacter(character)
local playerPosition = getPlayerPosition(player)
if playerPosition then
-- Parse the position string
local x, y, z = playerPosition:match("([^,]+),([^,]+),([^,]+)")
if x and y and z then
-- Teleport the player to the saved position
character:SetPrimaryPartCFrame(CFrame.new(Vector3.new(tonumber(x), tonumber(y), tonumber(z))))
else
warn("Invalid player position format:", playerPosition)
end
else
warn("No position saved for player:", player.Name)
end
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(function(character)
onPlayerRespawn(character)
character:WaitForChild("Humanoid").Died:Connect(function()
savePlayerPosition(player)
end)
end)
end
-- Connect the onPlayerAdded function to the PlayerAdded event
game.Players.PlayerAdded:Connect(onPlayerAdded)
-- Handle players that were already in the game when the script runs
for _, player in ipairs(game.Players:GetPlayers()) do
onPlayerAdded(player)
end
-- Define specific checkpoint objects here
local platform = script.Parent
local platformArea = platform.platformArea
local arcsVFX = script.Parent:FindFirstChild("B - Arcs v1 [C]").Middle.Arcs
local checkpointParts = {
platform.Arrow,
platform.Part1,
platform.Part2,
platform.Part3
}
local colorsToChange = {
Color3.fromRGB(0, 255, 0),
Color3.fromRGB(0, 255, 0),
Color3.fromRGB(255, 255, 255),
Color3.fromRGB(255, 255, 255)
}
local defaultColor = {
Color3.fromRGB(196, 40, 28),
Color3.fromRGB(248, 248, 248),
Color3.fromRGB(196, 40, 28),
Color3.fromRGB(248, 248, 248)
}
-- Variable to track if the event is already being processed
local debounce = false
local animationId = "rbxassetid://507771019"
local soundId = "rbxassetid://1843468464"
-- Connect the function to the Touched event of the checkpoint
platformArea.Touched:Connect(function(hit)
if debounce then
return -- If an event is already in progress, exit the function
end
debounce = true -- Mark that an event is in progress
print("Part touched")
-- Change the colors of checkpoint parts when touched
changeColors(checkpointParts, colorsToChange)
-- Check if the object that touched the part is a character (player)
local character = hit.Parent:FindFirstChildOfClass("Humanoid")
if character and game.Players:GetPlayerFromCharacter(hit.Parent) then
print("Player character found")
-- Call the coroutine functions with the player and the animation/sound parameters
spawn(function()
playAnimationFromHumanoid(character, animationId, 3) -- Duration of 3 seconds
end)
spawn(function()
playSound(soundId, 3) -- Duration of 3 seconds
end)
spawn(function()
enableVFXForDuration(arcsVFX, 3) -- Duration of 3 seconds
end)
savePlayerPosition(game.Players:GetPlayerFromCharacter(hit.Parent)) -- Save player position
else
print("Not a player character")
end
wait(2) -- Wait for a moment to prevent the event from firing multiple times quickly
debounce = true -- Reset the event in progress indicator to allow it to fire again
end)
-- Connect the function to the TouchEnded event of the checkpoint
platformArea.TouchEnded:Connect(function()
-- Reset the default colors when the checkpoint touch ends
--changeColors(checkpointParts, defaultColor)
-- Disable the VFX if it's currently active
if arcsVFX then
arcsVFX.Enabled = false
end
end)