Setting the parent property of a parent

Hey developers!

When I try to move my coin to a folder for inactive coins (coins that already have been collected in this round and will be restored in the next round) by setting the Parent property, it seems to get a little confused. I want to refer to the Parent of the script by using script.Parent, but also wanna set the Parent property of that instance by using .Parent = after. Any help would be appreciated!

local originalparent = script.Parent
local coins = game:GetService('Workspace').CoinsCollected
local coinsfolder = game:GetService('ReplicatedStorage').InactiveCoins
local restart = game:GetService('ReplicatedStorage').Restart

script.Parent.Touched:Connect(function(part)
	if part.Parent:FindFirstChildOfClass('Humanoid') then
		coins.Value += 1
		script.Parent.Parent = coinsfolder
	end
end)

restart.OnServerEvent:Connect(function()
	script.Parent.Parent = originalparent
end)

Have a great day, iamajust

By the way, it is a single-player game. Just saying to avoid confusion around global values and such.

just do

script.Parent = thing2

Does that set the Parent property of the Parent of the script? Or does it just set the Parent property of the script itself?

That sets the parent of the script, what is the parent of the script btw

A cylinder part that is used as a coin.

script it self but u can do

script.Parent.Parent = thing2

Ok so a few things I would recommend.

  1. Only have one script, in ServerScriptService, that handles the coin pickup, this will make the game less laggy. Something like
local coinsFolder = game.Workspace.Map.Coins:GetChildren()
local inactiveCoins = game.Workspace.Map.InactiveCoins:GetChildren()

for _, coin in pairs(coinsFolder) do
    if not coin:IsA("BasePart") then continue end
    coin.Touched.Connect(function(hit)
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            -- Add coins
            coin.Parent = inactiveCoins
        end
    end
end

That’s what I tried but it seems to replace the instance instead of setting the Parent property of the instance.

bro wth are you even saying? are you trying to set the parent property of the parent of the script?

Yes, that’s what I’m trying to do.

then just do

script.Parent.Parent = -- path to the new parent

I know that’s what you think is the correct way to do it, I tried that too, but it doesn’t work. It just deletes the instance.

are you even setting what it should be?

Have you tried my suggestion?
Chars

Yes! Try it yourself by making two folders, inserting a part into one of them, then inserting a script that tries to move the part to the other folder that way, run it, and you’ll see the instance dissapears.

@iamajust have you tried my suggestion, not only should it work it should also save performance

I think its really stupid to keep saying that “try my suggestion” you need to wait for your turn. Ok? and @iamajust it works for me try using a separate script

I will try, but do you recommend also setting health to 0 and handling tweens for all coins this way? (Setting health to 0 is for traps.)

If their in a script in the coins right now, then yes change that.

I don’t get what you mean by this.