Problem solved and fixed

I thought to put it in local script of tool and use WaitForChild:()

1 Like

How does your server script look like now?

script with action after reset

local background = workspace.BloomBackground
local template = workspace.Bloom_Transform
local crown = template.Bloom_MW_crown
local shirt = template.Shirt
local pants = template.Pants
local wing_l = template.Wing_Left
local wing_r = template.Wing_Right
local effect = template.RightHand.Attachment.Fire

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://15723632456"

game:GetService("ReplicatedStorage").AmberS1.OnServerEvent:Connect(function(player)
	if player.Character then
		local tool = player.Character:FindFirstChild("Magic Finix Amber")

		if tool then
			local character = player.Character
			local humanoid = character.Humanoid

			tool.Handle.Attachment.Fire.Enabled = false

			local animation = animation:Clone()
			animation.Parent = tool

			character.PrimaryPart.Anchored = true
			humanoid.WalkSpeed = 0
			humanoid.JumpHeight = 0

			local clonedBackground = background:Clone()
			clonedBackground.Name = "localBackground"
			clonedBackground.CFrame = character.PrimaryPart.CFrame * CFrame.new(0, 0, 2)
			clonedBackground.Rotation = character.PrimaryPart.Rotation
			clonedBackground.Parent = workspace
		
			humanoid.Died:Once(function() -- Add things you want to destroy when player dies/resets in here
				clonedBackground:Destroy()
			end)
			game.ReplicatedStorage.ChangePlayerCharacter.OnServerEvent:Connect(function()
				clonedBackground:Destroy()
			end)

			local cloneeffect1 = effect:Clone()
			cloneeffect1.Parent = character.RightHand

			local cloneeffect2 = effect:Clone()
			cloneeffect2.Parent = character.LeftHand

			local music = clonedBackground.MagicWinx
			music.Playing = true

			local track = humanoid.Animator:LoadAnimation(animation)
			track:Play()
			task.wait(3)

			cloneeffect1.Enabled = true
			cloneeffect2.Enabled = true
			task.wait(1.5)
			cloneeffect1.Enabled = false
			cloneeffect2.Enabled = false

			local shirtID = shirt.ShirtTemplate
			character.Shirt.ShirtTemplate = shirtID
			character.Pants.PantsTemplate = "rbxassetid://15724231382"
			task.wait(2)

			local pantsID = pants.PantsTemplate
			character.Pants.PantsTemplate = pantsID
			task.wait(3.75)

			local wing_lclone = wing_l:Clone()
			wing_lclone.Handle.AccessoryWeld.Part1 = character.UpperTorso
			wing_lclone.Parent = character

			local wing_rclone = wing_r:Clone()
			wing_rclone.Handle.AccessoryWeld.Part1 = character.UpperTorso
			wing_rclone.Parent = character
			task.wait(1)

			local crownclone = crown:Clone()
			crownclone.Handle.AccessoryWeld.Part1 = character.Head
			crownclone.Parent =  character
			task.wait(2.5)

			cloneeffect1.Enabled = true
			cloneeffect2.Enabled = true
			task.wait(1)
			cloneeffect1.Enabled = false
			cloneeffect2.Enabled = false
			task.wait(6)

			clonedBackground:Destroy()

			humanoid.WalkSpeed = 16
			humanoid.JumpHeight = 7.2
			character.PrimaryPart.Anchored = false


			tool:Destroy()
		else
			warn("Magic Finix Amber isn't equipped!")
		end
	else
		warn("player.Character is currently nil")
	end
end)

I noticed you wrote:

game.ReplicatedStorage.ChangePlayerCharacter.OnServerEvent:Connect(function()
	clonedBackground:Destroy()
end)

This is the cause of the problems you’re experiencing because that event is firing regardless of who activated the tool

The humanoid.Died:Once should be enough to destroy the clonedBackground on its own when the tool is activated and the character resets/dies

I tested that and without this it didn’t work

so I think to put this in local

What are you trying to achieve though, please be descriptive so I can help you better

Do you want to destroy the clonedBackground when the character dies/resets?

yes and when player uses event bc it changes character

When I only use humanoid.Died then it only reset when player dead but when player changes character then not.

This should work now:

local debris = game:GetService"Debris"

local background = workspace.BloomBackground
local template = workspace.Bloom_Transform
local crown = template.Bloom_MW_crown
local shirt = template.Shirt
local pants = template.Pants
local wing_l = template.Wing_Left
local wing_r = template.Wing_Right
local effect = template.RightHand.Attachment.Fire

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://15723632456"

local clonedBackgrounds = {}

game:GetService("ReplicatedStorage").AmberS1.OnServerEvent:Connect(function(player)
	if player.Character then
		local tool = player.Character:FindFirstChild("Magic Finix Amber")

		if tool then
			local character = player.Character
			local humanoid = character.Humanoid

			tool.Handle.Attachment.Fire.Enabled = false

			local animation = animation:Clone()
			animation.Parent = tool

			character.PrimaryPart.Anchored = true
			humanoid.WalkSpeed = 0
			humanoid.JumpHeight = 0

			local clonedBackground = background:Clone()
			clonedBackground.Name = "localBackground"
			clonedBackground.CFrame = character.PrimaryPart.CFrame * CFrame.new(0, 0, 2)
			clonedBackground.Rotation = character.PrimaryPart.Rotation
			clonedBackground.Parent = workspace

			clonedBackgrounds[player] = clonedBackground

			humanoid.Died:Once(function() -- Add things you want to destroy when player dies/resets in here
				debris:AddItem(clonedBackgrounds[player], 0)
				clonedBackgrounds[player] = nil
			end)

			local cloneeffect1 = effect:Clone()
			cloneeffect1.Parent = character.RightHand

			local cloneeffect2 = effect:Clone()
			cloneeffect2.Parent = character.LeftHand

			local music = clonedBackground.MagicWinx
			music.Playing = true

			local track = humanoid.Animator:LoadAnimation(animation)
			track:Play()
			task.wait(3)

			cloneeffect1.Enabled = true
			cloneeffect2.Enabled = true
			task.wait(1.5)
			cloneeffect1.Enabled = false
			cloneeffect2.Enabled = false

			local shirtID = shirt.ShirtTemplate
			character.Shirt.ShirtTemplate = shirtID
			character.Pants.PantsTemplate = "rbxassetid://15724231382"
			task.wait(2)

			local pantsID = pants.PantsTemplate
			character.Pants.PantsTemplate = pantsID
			task.wait(3.75)

			local wing_lclone = wing_l:Clone()
			wing_lclone.Handle.AccessoryWeld.Part1 = character.UpperTorso
			wing_lclone.Parent = character

			local wing_rclone = wing_r:Clone()
			wing_rclone.Handle.AccessoryWeld.Part1 = character.UpperTorso
			wing_rclone.Parent = character
			task.wait(1)

			local crownclone = crown:Clone()
			crownclone.Handle.AccessoryWeld.Part1 = character.Head
			crownclone.Parent =  character
			task.wait(2.5)

			cloneeffect1.Enabled = true
			cloneeffect2.Enabled = true
			task.wait(1)
			cloneeffect1.Enabled = false
			cloneeffect2.Enabled = false
			task.wait(6)

			debris:AddItem(clonedBackground, 0)

			humanoid.WalkSpeed = 16
			humanoid.JumpHeight = 7.2
			character.PrimaryPart.Anchored = false


			tool:Destroy()
		else
			warn("Magic Finix Amber isn't equipped!")
		end
	else
		warn("player.Character is currently nil")
	end
end)

game.ReplicatedStorage.ChangePlayerCharacter.OnServerEvent:Connect(function(player)
	debris:AddItem(clonedBackgrounds[player], 0)
	clonedBackgrounds[player] = nil
end)

It worked now. Thank you so much!!!

2 Likes

Glad I could help, and merry Christmas! :grin:

Thanks!!! Same to you!!!:christmas_tree::christmas_tree::christmas_tree::christmas_tree::christmas_tree::christmas_tree::christmas_tree::christmas_tree::christmas_tree::christmas_tree::christmas_tree::christmas_tree:

2 Likes

And I have question, if you could add there if player leave the game too.

is it something like what you write with remoteevent but PlayerRemove?

You’ll need to use the Players.PlayerRemoving event. I suggest you write it below the ChangePlayerCharacter function/connection like so:

game.ReplicatedStorage.ChangePlayerCharacter.OnServerEvent:Connect(function(player)
	debris:AddItem(clonedBackgrounds[player], 0)
	clonedBackgrounds[player] = nil
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
	-- Write what you want to happen when a player leaves in here
	-- Example: print(player.Name)
end)

so if I want then delete part then I can copy that debris from event?

Correct, here’s an example:

game:GetService("Players").PlayerRemoving:Connect(function(player)
	debris:AddItem(Part, 4) -- Part is the part you want to destroy, and 4 is the duration of time before it's destroyed (You can set it to 0 instead of 4 if you want the Part to be destroyed very quickly)
end)

By the way, the reason why I prefer to use debris:AddItem() instead of :Destroy() is because debris doesn’t error if the Part or Instance you want to destroy happens to already have been destroyed, so using it means your code is less likely to error :slightly_smiling_face::+1:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.