Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Im Very confused why this keeps happening can someone tell me why?

Asked by 4 years ago

I made a script trying to regen models and its not working BUT this keeps happening a LOT during scripting can someone please tell me why before i lose my sanity? what it does every minute it is supposed to clone it into Replicated Storage then it Destroys the existing one and then it waits for the child of Replicated Storage of the testhouse now i know i maybe done a lot wrong since i am still pretty new to scripting here is the script:

local REGEN = game.Workspace.TestHouse
local MODEL_BACKUP = REGEN:Clone()
local work = game.Workspace

while true do 
    wait(60)
    MODEL_BACKUP = game.ReplicatedStorage
    REGEN:Destroy()
    game.ReplicatedStorage:WaitForChild(REGEN) = work


end
1
It's extremely difficult to understand what you're trying to do. Please consider rewording your question. crywink 419 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Nothing wrong with being new to scripting, we were all there.

In order to shift where the cloned object is, you have to change the parent property of the object. So instead of doing "MODEL_BACKUP = game.ReplicatedStorage" you would type "MODEL_BACKUP.Parent = game.ReplicatedStorage"

The second thing is on line 9, you say "WaitForChild(REGEN)" which will not work for several reasons.

*REGEN was destroyed and has it's parent value locked to nil *WaitForChild take a string, and you're passing in an object

If you wanted to wait for the new TestHouse, you would just do "game.ReplicatedStorage:WaitForChild("TestHouse")" or you could even do "game.ReplicatedStorage:WaitForChild(MODEL_BACKUP.Name)"

The second one uses the name of MODEL_BACKUP as it should be the same as REGEN.Name which will be "TestHouse".

Lastly we need to talk about how you're setting the variables and how since this is a loop will eventually fail. How your code is written, after we changed everything to conform to Lua, will still have a logical problem. Every minute, it makes MODEL_BACKUP which is the original clone of REGEN and we set it's parent to ReplicatedStorage. Then we destroy REGEN, but on the second go destroying the REGEN variable doesn't make sense, we already did it once.

We can fix these logical errors by thinking back to what you wanted to do in the first place: Clone an object, move the clone to ReplicatedStorage. Destroy the original object in workspace, and move the clone into workspace.

In this case, I can rewrite most of the logic fairly easily.

This whole thing could be rewritten as:

local REGEN = game.Workspace.TestHouse
local MODEL_BACKUP = REGEN:Clone()
local work = game.Workspace

while true do 
    wait(60)
    --//When you clone an object, it sets it's parent to nil, so we don't really need to move it anywhere unless you need it in ReplicatedStorage for a reason.

    --//we destroy the original object, and parent the clone to workspace
    REGEN:Destroy()
    MODEL_BACKUP.Parent = work

    --//Next we reassign REGEN as MODEL_BACKUP and repeat what we did at the top of the screen. This will ensure that when the loop comes around again everything work as expected.
    REGEN = MODEL_BACKUP
    MODEL_BACKUP = REGEN:Clone()
end

If you have any other questions feel free to message me.

0
Great Explanation! JayShepherdMD 147 — 4y
0
Thank you great explanation and you helped me leanr something new and solved my question! Vortex_Vasne 89 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I see what the problem is, (I believe). I think you're not defining the parent of the model you're trying to spawn. This should help!

local REGEN = game.Workspace.TestHouse
local MODEL_BACKUP = REGEN:Clone()
local work = game.Workspace

while true do 
    wait(60)
    MODEL_BACKUP.Parent = game.ReplicatedStorage
    REGEN:Destroy()
    game.ReplicatedStorage:WaitForChild("MODEL_BACKUP").Parent = game.Workspace


end

I'm still a little confused on what you're trying to do, but if this works let me know.

Answer this question