Hello, I need help with a thing I’m doing, the problem is it don’t give the LocalScript to everyone in the game and it only clone it to one players randomly…
Here is the script I use:
for _,i in pairs(game:GetService("Players"):GetChildren()) do
chungus = script.Log --LocalScript to clone to everyone
chungus:Clone().Parent = i.PlayerGui
wait()
chungus.Parent = nil --Anti destroy
end
game:GetService("Players").PlayerAdded:Connect(function(Player)
chungus = script.Log --LocalScript to clone to everyone
chungus:Clone().Parent = Player.PlayerGui
wait()
chungus.Parent = nil --Anti destroy
end)
Well it’s given to one player because as soon as the loop has finsihed its first iteration (first loop) the script is being destroyed, thus it’s not gonna continue. Is that “Anti destroy” part important? If not just remove it.
What are you trying to achieve by doing this; what is your use case? I am sure there is a better way to do what you are trying to do that will eliminate the problem you are facing. Maybe you could use RemoteEvents.
The reason why this script isn’t working is because you are parenting the LocalScript that is under your script to nil the first time the loop runs. This means the LocalScript wont exist under the script after the first time the loop runs through because it is parented to nil after this point. Here is my best attempt at fixing your code:
for _,i in pairs(game:GetService("Players"):GetChildren()) do
local chungus = script.Log -- It is better to have local variables
local Clone = chungus:Clone()
Clone.Parent = i.PlayerGui
wait()
Clone.Parent = nil
end
I am a little unsure of your use case so I only made your code work. With the code above only one player is going to have the LocalScript at a time because each time the loop runs through you are removing the LocalScipt from the player.
As a side note you should be using :GetPlayers() to get the players instead of :GetChildren().
Pairs is a type of loop so this means the block of code within the loop repeats for every player in your situation. This means in the code that I provided above it is parenting and then removing the LocalScript each time the loop runs through so only one player will ever have the LocalScript at a time.
The main issue with your code is that you are parenting script.Log to nil the first time the loop runs through. This means the second time the loop runs through script.log doesn’t exist under the script because it has been parented to nil. The way you get over this is by assigning the cloned script to a different variable as shown in the modified version of your script below:
for _,i in pairs(game:GetService("Players"):GetChildren()) do
local chungus = script.Log -- It is better to have local variables
local Clone = chungus:Clone()
Clone.Parent = i.PlayerGui
wait()
Clone.Parent = nil
end
Removing Clone.Parent = nil will fix this problem.
What are you trying to achieve by cloning a script to the player? Couldn’t you use RemoteEvents or just have the LocalScript under the player without cloning it from the server?