Script to clone a model to the player crashes everything

I created a script to clone a model to the player on every spawn, yet when I test it ingame, it brings up an error saying “Check your internet connection and try again” every single time. Disabling the script makes the game work fine, however testing inside studio, crashes all my windows, and force quits studio.

while true do game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(char) local sphere = game.ReplicatedStorage.TheSphere:Clone() sphere.Parent = char.Torso wait(10) sphere:Destroy() end) end) end

Un Lua-d script:

while true do
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local sphere = game.ReplicatedStorage.TheSphere:Clone()
sphere.Parent = char.Torso
wait(10)
sphere:Destroy()
end)
end)
end

Any help is appreciated, as this is a bit of an important task for the script to do.

Devforum removed tabs from the script, this is what it is supposed to look like.
image

Have you tried testing it without the loop?

I’ll try, but I only added that because I would think it is going to repeat on spawn.

I was able to get into the game, but it doesn’t clone anything.

try this

local sphere = game:GetService('ReplicatedStorage'):WaitForChild('TheSphere'):Clone()

Alright I’ll test it in game since studio takes a long time to load

That also didn’t really seem to work… I think I need to weld the model to the player, its a model with 3 parts welded together if that helps any.

You don’t need the while true do loop - the event fires every time a player joins the game.

However, you’ll need to account for if a player dies and respawns, because it completely changes the character. You should check out the Humanoid.Died event, and maybe add in a CharacterAdded there as well.

1 Like

So, it worked, but now it only clones when the player dies.
This is the updated code.image

You should probably move the code inside the .Died event into a separate function, and call the function twice, one time when the character is added the first time (in between CharacterAdded and Died) and when the character dies (inside the Died).

The question that I don’t know the answer to is whether or not it’ll work after multiple respawns, because when the player dies, the character is completely remade (so the old char that your script refers to technically doesn’t exist anymore).

By the way, does this need to be on the Server side? It would be 10x easier if you made it a LocalScript and put it inside StarterCharacterScripts (that will run every time a character is added, either when joining or after respawning). It just might not show up for other players.

If what I’m aiming for is trying to be accurate, I could definitely do a local script and only run that script for singleplayer levels.