Hi, I have a problem when sometimes join into the game I cant move, but it rarely happens and the only way to move is to rejoin the game, I think the problems is something with the humanoidrootpart, but im not sure how to fix.
local Players = game:GetService("Players")
local PhysicsService = game:GetService("PhysicsService")
local SoundService = game:GetService("SoundService")
local charactersFolder = game.ServerStorage:WaitForChild("StarterCharacters")
local characters = charactersFolder:GetChildren()
-- Collision group setup
local GroupName = "Players"
PhysicsService:RegisterCollisionGroup(GroupName)
PhysicsService:CollisionGroupSetCollidable(GroupName, GroupName, false)
-- Function to assign collision groups to character parts
local function assignCollisionGroup(character)
for _, part in ipairs(character:GetDescendants()) do
if part:IsA("BasePart") then
part.CollisionGroup = GroupName
end
end
end
-- Function to spawn the player at their saved stage
local function spawnPlayerAtStage(player, character)
local stage = player:WaitForChild("leaderstats"):WaitForChild("Stage").Value
-- Find the checkpoint corresponding to the stage
local checkpoint = workspace.Checkpoints:FindFirstChild(tostring(stage))
if checkpoint then
local spawnCFrame = checkpoint.CFrame + Vector3.new(0, 3.25, 0)
character:SetPrimaryPartCFrame(spawnCFrame)
else
warn("Checkpoint for stage " .. stage .. " not found!")
end
end
-- Function to randomize the character
local function randomizeCharacter(player)
if #characters > 0 then
local starterChars = {}
for _, char in ipairs(characters) do
table.insert(starterChars, char)
end
-- Randomly select a character
local random = Random.new()
local selectedCharacter = starterChars[random:NextInteger(1, #starterChars)]:Clone()
-- Wait for the original character to fully load
local oldCharacter = player.Character
if not oldCharacter or not oldCharacter:FindFirstChild("HumanoidRootPart") then
player.CharacterAdded:Wait()
repeat
task.wait(0.2) -- Keep checking until the character loads
until player.Character and player.Character:FindFirstChild("HumanoidRootPart")
oldCharacter = player.Character
end
-- Get the old character's position
local oldCFrame = oldCharacter:GetPrimaryPartCFrame()
selectedCharacter:SetPrimaryPartCFrame(oldCFrame)
oldCharacter:Destroy()
-- Assign the new character
player.Character = selectedCharacter
selectedCharacter.Parent = workspace
-- Assign collision group
assignCollisionGroup(selectedCharacter)
return selectedCharacter
else
warn("No starter characters available in StarterCharacters folder.")
return player.Character
end
end
-- Function to reconnect tool sound after respawn with cooldown
local function reconnectToolSound(player)
task.wait(1) -- Wait for the tool to load in Backpack
local tool = player.Backpack:FindFirstChild("Speaker") -- Replace with your actual tool's name
if tool then
local sound = SoundService:FindFirstChild("QuackSound") -- Ensure the sound exists
local reconnectSound = SoundService:FindFirstChild("ReconnectSound") -- New reconnect sound
if sound then
tool.Activated:Connect(function()
-- Cooldown check to prevent immediate use
if not tool:GetAttribute("Cooldown") then
tool:SetAttribute("Cooldown", true) -- Set cooldown attribute to true
sound:Play() -- Play the original tool sound
-- Reset cooldown after 10 seconds
task.wait(10)
tool:SetAttribute("Cooldown", false) -- Reset cooldown
end
end)
else
warn("QuackSound not found in SoundService!")
end
if reconnectSound then
tool.Activated:Connect(function()
-- Cooldown check to prevent immediate use
if not tool:GetAttribute("Cooldown") then
tool:SetAttribute("Cooldown", true) -- Set cooldown attribute to true
reconnectSound:Play() -- Play the reconnect sound
-- Reset cooldown after 10 seconds
task.wait(10)
tool:SetAttribute("Cooldown", false) -- Reset cooldown
end
end)
else
warn("ReconnectSound not found in SoundService!")
end
end
end
-- Player Added logic
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Randomize the character
local randomizedCharacter = randomizeCharacter(player)
-- Spawn the player at their saved stage
spawnPlayerAtStage(player, randomizedCharacter)
-- Double-check the spawn position after a delay to ensure correctness
task.delay(1, function()
spawnPlayerAtStage(player, randomizedCharacter)
end)
-- Reconnect tool functionality after respawn
reconnectToolSound(player)
end)
end)
-- Save player's progress when they reach a checkpoint
workspace.Checkpoints.ChildAdded:Connect(function(checkpoint)
if checkpoint:IsA("Part") then
checkpoint.Touched:Connect(function(hit)
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)
if player then
local stage = tonumber(checkpoint.Name)
if stage then
player.leaderstats.Stage.Value = stage
print("Player " .. player.Name .. " reached stage " .. stage)
end
end
end)
end
end)
-- Reset player's stage to 0
script.Parent.MouseButton1Click:Connect(function()
local player = script:FindFirstAncestorOfClass("Player")
if player then
player.leaderstats.Stage.Value = 0 -- Reset leaderstats.Stage
print("Player " .. player.Name .. " reset to stage 0.")
local character = player.Character
local checkpoint = workspace.Checkpoints:FindFirstChild("0")
if checkpoint and character then
character:SetPrimaryPartCFrame(checkpoint.CFrame + Vector3.new(0, 3, 0))
else
warn("Checkpoint for stage 0 not found!")
end
end
end)