Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[FIXED]How to fix a script that loops and re-enables without any lines telling it to loop?

I am trying to make a script that randomly selects an online friend and turns your character into theirs, but for some reason the script loops and there aren't any while, for, or repeat loops in the script. The looping causes my character to constantly change. The script also re-enables even after I put script.Disabled = true. What's causing this problem is line 11, and deleteing the script doesn't work either. It "comes back to life" and keeps looping

this is the localscript

wait()
local plr = game.Players.LocalPlayer

local friend = plr:GetFriendsOnline(200)

local one = math.random(1,#friend)
	local chosenOne = friend[one]
	print(tostring(chosenOne.UserName))
	
print(tostring(chosenOne.VisitorId))
game.ReplicatedStorage.AddFriendChar:FireServer(chosenOne.VisitorId)
script.Disabled = true

this is the script in ServerScriptService

game.ReplicatedStorage.AddFriendChar.OnServerEvent:Connect(function(p, id)
	p.CharacterAppearanceId = id
	p:LoadCharacter()
end)

2 answers

✓ Accepted answer

I figured out the reason: the script was "coming back to life" because the character reloaded, causing the entire player to reload too, causing the script to loop. The solution was to add a boolvalue in the workspace so that once the new character is loaded, the boolvalue is checked, and if it's checked the script cannot run

wait()
local plr = game.Players.LocalPlayer

local friend = plr:GetFriendsOnline(200)

local one = math.random(1,#friend)
	local chosenOne = friend[one]
	print(tostring(chosenOne.UserName))
	
print(tostring(chosenOne.VisitorId))
if workspace.LoadedIn.Value == false then
game.ReplicatedStorage.AddFriendChar:FireServer(chosenOne.VisitorId)
end

Instead of adding a BoolValue to workspace, you could simply place the script in StarterPlayerScripts, located under StarterPlayer. It will not reload, nor will it refresh everytime you load.

This discussion is preserved as it was. New answers, comments, and votes are closed.