The question is simple, but what’s the answer? I’m currently trying to do this, but keep getting this warning:
I’ve gotten this warning before, after trying to set the parent of the character of the character instantly after it spawned. Anyway I can prevent this without having to set up some unnecessary remoteevent system?
It is generally not recommended to set the parent of a script inside of itself, as this can cause unexpected behavior and may lead to infinite recursion.
If you want to set the parent of a script, you can do so by assigning the desired parent object to the Parent property of the script. For example:
local script = script -- the current script
local newParent = game.Workspace -- the new parent object
-- Set the script's parent to the new parent object
script.Parent = newParent
Keep in mind that changing the parent of a script may affect its execution, as the script will now be run in the context of its new parent. For example, if the script accesses any global variables or functions, they may be different in the new parent than they were in the original parent.
If you want to modify the parent of a script inside of the script itself, it is generally a better idea to do so in a separate function or method, rather than within the script’s main code block. This can help avoid potential issues with infinite recursion or other unexpected behavior.
I hope this helps! Let me know if you have any further questions.
This works, but it starts an infinite loop where i can’t even move because my character keeps getting reset. Here’s my script:
if (not genlever1.IsEngaged.Value) or (not genlever2.IsEngaged.Value) then
return
end
wait(0.03)
if (not model.IsEngaged.Value) then--or (not seat.Occupant) then
return
end
local _char = plr.Character
local newchar = zombiemodel:Clone()
local cframe = _char:GetPrimaryPartCFrame()
newchar.Name = plr.Name
newchar:SetPrimaryPartCFrame(cframe)
newchar.Head.face.Texture = _char.Head.face.Texture
newchar.Parent = game.Workspace
newchar["Body Colors"]:Destroy()
_char["Body Colors"].Parent = newchar
local shirt = Instance.new("Shirt", newchar)
shirt.ShirtTemplate = _char.Shirt.ShirtTemplate
local pants = Instance.new("Pants", newchar)
pants.PantsTemplate = _char.Pants.PantsTemplate
for _, v in newchar:GetChildren() do
if v:IsA("BasePart") and not (v.Name == "HumanoidRootPart") then
v.CanCollide = true
v.Transparency = 0
v.CanTouch = true
v.CanQuery = true
end
end
plr.Character = newchar
newchar.Animate.Enabled = true
_char:Destroy()
local __script = _script:Clone()
__script.Parent = plr.Character
__script.Player.Value = plr
__script.Enabled = true
local plr = script.Player.Value
local stage = nil
local zombiemodel = game.Workspace.FemurBreaker.EffectResources["Zombie R15"]
local _script = game.Workspace.FemurBreaker.EffectResources.ZombieScript
local mutatetime = 30 --6 * 60 -- six minutes in seconds
function toZombie(_plr)
local _char = _plr.Character
local newchar = zombiemodel:Clone()
local cframe = _char:GetPrimaryPartCFrame()
newchar.Name = _plr.Name
newchar:SetPrimaryPartCFrame(cframe)
newchar.Head.face.Texture = _char.Head.face.Texture
newchar.Parent = game.Workspace
newchar["Body Colors"]:Destroy()
_char["Body Colors"].Parent = newchar
local shirt = Instance.new("Shirt", newchar)
shirt.ShirtTemplate = _char.Shirt.ShirtTemplate
local pants = Instance.new("Pants", newchar)
pants.PantsTemplate = _char.Pants.PantsTemplate
for _, v in newchar:GetChildren() do
if v:IsA("BasePart") and not (v.Name == "HumanoidRootPart") then
v.CanCollide = true
v.Transparency = 0
v.CanTouch = true
v.CanQuery = true
end
end
_plr.Character = newchar
newchar.Animate.Enabled = true
if _plr == plr then
task.wait()
script.Parent = newchar
stage = script.Parent.Stage
else
_script:Clone().Parent = newchar
end
wait(1)
_char:Destroy()
end
toZombie(plr)
The first script initializes the process, and clones the second script. The second script is supposed to reparent it self to another character. but it keeps doing it.
I encountered a similar problem just 30 minutes ago. To fix it, I just added this line of code right before I set the parent of the object I was attempting to change:
RunService.Heartbeat:Wait()
This worked for me. I assume that RunService.RenderStepped would work just as well.
I don’t know if this will work for you, but it did for me
One way is to use WaitForChild() to make sure that the parent object is loaded. This can be used to make sure that the script is not being set as the parent of itself. In your case, this would be something like this: script.Parent = script:WaitForChild(“Part”) Another way is to pass a script as an argument to the function you’re using to set the parent. This is basically the same as the first way, but it’s a little more general. Here’s an example of how to do this: function setParent(script, parent) local newParent = parent if type(parent) == “string” then newParent = script:WaitForChild(parent) end script.Parent = newParent end setParent(script, “Part”) – This is the same as the first example. setParent(script, script.Parent) – This is the same as the second example. You could also use a remote event to do this, as you mentioned. However, if you use a remote event, you need to make sure that the script will wait for the event to be fired before setting the parent. Here’s an example: local event = Instance.new(“RemoteEvent”, script.Parent) event.Name = “SetParent” event.OnServerEvent:Connect(function(player, script, parent) local newParent = parent if type(parent) == “string” then newParent = script:WaitForChild(parent) end script.Parent = newParent end) event:FireServer(script, “Part”) – This is the same as the first example. event:FireServer(script, script.Parent) – This is the same as the second example.
Questions:
Answers:
You’re trying to set the parent of a script to itself. You can’t do that. You’ll get a warning every time you do that. The warning is to let you know that you’re doing something you don’t want to do.
Questions:
Answers:
I think I can answer the question. You can set the parent of a script inside of itself. This is what I did: script.Parent = script.Parent.Parent This didn’t give me the warning, but I think it
I solved the problem, but that doesn’t mean this thread is solved. We still need a valid way to set the parent of a script inside itself, maybe I just haven’t tried out all of the answers recommended to me, but they haven’t worked.
i’ve done this before without much of an issue, i think the warning is because you’re doing it at the start of the script as its being parents so it doesn’t want to move so quickly. when i had this happen i just literally put task.wait for like .2 seconds b4 reparenting