Hello, i have a script that checks if players character is added, and then it calls a remote event that changes the players character and it keeps looping. Its located in StarterPack so it does it a again when the played dies
Local Script:
local player = game.Players.LocalPlayer
local MaleChar = game:GetService("ReplicatedStorage").MaleModel
local FemaleChar = game:GetService("ReplicatedStorage").FemaleModel
local IsOn = false
player.CharacterAdded:Connect(function()
if player.PlrStats.Trait.Value == "Female" then
print("worked")
script.RemoteEvent:FireServer(FemaleChar, IsOn)
elseif player.PlrStats.Trait.Value == "Male" then
print("worked")
script.RemoteEvent2:FireServer(FemaleChar, IsOn)
end
end)
serverscript:
script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, FemaleChar, IsOn)
print("is a female")
local oldCharacter = player.Character
local morphModel = FemaleChar
local newCharacter = morphModel:Clone()
newCharacter.HumanoidRootPart.Anchored = false
newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)
newCharacter.Name = player.Name
player.Character = newCharacter
newCharacter.Parent = workspace
oldCharacter.Animate.Parent = newCharacter
if newCharacter:FindFirstChild("RoxieMaleShortEyelash" or "RoxieMaleRoundEyebrow") then
newCharacter.RoxieFemaleLongEyelash:Destroy()
newCharacter.RoxieFemaleAngledEyebrow:Destroy()
end
end)
script.Parent.RemoteEvent2.OnServerEvent:Connect(function(player, MaleChar, IsOn)
print("is a male")
local oldCharacter = player.Character
local morphModel = MaleChar
local newCharacter = morphModel:Clone()
newCharacter.HumanoidRootPart.Anchored = false
newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)
newCharacter.Name = player.Name
player.Character = newCharacter
newCharacter.Parent = workspace
oldCharacter.Animate.Parent = newCharacter
if newCharacter:FindFirstChild("RoxieFemaleLongEyelash" or "RoxieFemaleAngledEyebrow") then
newCharacter.RoxieFemaleLongEyelash:Destroy()
newCharacter.RoxieFemaleAngledEyebrow:Destroy()
end
end)
“CharacterAdded” runs whenever a character is added. This includes when the server replaces the old character with the new one.
A quick bandaid fix would be to add a debounce for it
local debounce
player.CharacterAdded:Connect(function()
if not debounce then
debounce = true
if player.PlrStats.Trait.Value == "Female" then
print("worked")
script.RemoteEvent:FireServer(FemaleChar, IsOn)
elseif player.PlrStats.Trait.Value == "Male" then
print("worked")
script.RemoteEvent2:FireServer(FemaleChar, IsOn)
end
task.wait(1)
debounce = false
end
end
It is a bandaid fix, though. I’d strongly recommend you instead get rid of the remote entirely, disable the “CharacterAutoLoads” property in Players and spawn players in directly as their character rather than waiting for their roblox one to load in first before replacing them with the custom one.
@Coolbro11741
I recommend Putting your Script inside StarterPlayerScripts and then doing this:
local CharA = game.Players.LocalPlayer.CharacterAdded:Connect(function()
-- Your Code here
end)
wait() -- Gives function time to run
CharA:Disconnect() -- Disconnects the Function
After that, CharacterAdded( or in this case “CharA”) should stop running
My solution was a bandaid fix, this one is as well. I’d still urge OP to change the way their characters are loaded so that it doesn’t pointlessly fire from the client and doesn’t need to rely on roblox spawning the player’s original character first.
Assuming you’re doing the bandaid fix; Where is your script located? It might just be reloading along with the character thus getting rid of the debounce.
Try just making a localscript in StarterCharacterScripts that just contains the stuff that’s running inside of the characteradded
local player = game.Players.LocalPlayer
local IsOn = false-- Not sure what this variable is used for, might cause issues?
if player.PlrStats.Trait.Value == "Female" then
print("worked")
script.RemoteEvent:FireServer(FemaleChar, IsOn)
elseif player.PlrStats.Trait.Value == "Male" then
print("worked")
script.RemoteEvent2:FireServer(FemaleChar, IsOn)
end
This script should only be added when a regular roblox character is added since you’re replacing them with custom characters, thus not containing this localscript and only running once
Put your remotes in ReplicatedStorage and call them something unique, such as “SpawnFemale” and “SpawnMale”.
Then swap out this section with
if player.PlrStats.Trait.Value == "Female" then
print("worked")
game.ReplicatedStorage.SpawnFemale:FireServer(FemaleChar, IsOn)
elseif player.PlrStats.Trait.Value == "Male" then
print("worked")
game.ReplicatedStorage.SpawnMale:FireServer(FemaleChar, IsOn)
end
Then put the server-sided script in ServerScriptService, and update the references to the remotes
Edit: Ideally your server script shouldn’t need to refer directly to itself in any case. It should just be a generic handler that is responsible for each player rather than an individual script existing for each individual player.