Hi Devforum users, I’m trying to code a loading screen character selection, but it seems that when i select a character, the script loops again and I never get off the loading screen.
The reason for this: The character is not currently in workspace when cloned, so when I set it’s parent to workspace
, it thinks the player is loading in again. I don’t know how to fix it, please help me guys!
---Server Sided script
local hasChosenCharacter = false
local function onCharacterAdded(character)
if hasChosenCharacter then return end
hasChosenCharacter = true
tweenA:Play()
tweenB:Play()
task.wait(1)
local function setupHover(button)
button.MouseEnter:Connect(function()
tweenSize(button)
hover()
end)
button.MouseLeave:Connect(function()
tweenSizeRegular(button)
task.wait(0.2)
end)
end
setupHover(marley)
setupHover(cocoa)
setupHover(ghost)
setupHover(mango)
for _,v in pairs(script.Parent:GetDescendants()) do
if v:IsA("ImageButton") then
v.MouseButton1Click:Connect(function()
if character:FindFirstChild("Humanoid") and character.Humanoid.Health > 0 and not deb then
deb = true
local characterName = v.Parent.Name
event:FireServer(characterName)
selectionGui:Destroy()
wait(1)
-- No need to allow reuse of the GUI
end
end)
end
end
end
local hasHandledCharacter = false
local function handleCharacter(character)
if hasHandledCharacter then return end
hasHandledCharacter = true
--print("Character handled:", character)
onCharacterAdded(character)
end
if player.Character then
task.defer(function()
onCharacterAdded(player.Character)
end)
else
player.CharacterAdded:Once(onCharacterAdded)
end
---Local script
local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("Remotes").Character
local ss = game:GetService("ServerStorage")
local characterFolder = ss:WaitForChild("CatCharacters")
event.OnServerEvent:Once(function(player, characterName)
if not characterName then
warn("characterName is nil!")
return
end
print("Received characterName:", characterName)
local ChosenCharacter = characterFolder:FindFirstChild(characterName)
if not ChosenCharacter then
warn("Character not found in folder:", characterName)
return
end
ChosenCharacter = ChosenCharacter:Clone()
player.Character = ChosenCharacter
task.wait(1)
ChosenCharacter = player.Name
end)
Apparently it works after a second time of clicking a character? This could be an easy fix and im just a bad scripter lol