Script not running after parent is cloned [SOLVED]

Hello! This is my first post on the Devforum and I’m excited to be here! Now concerning my problem, I’m trying to make a Sonic engine and I have the damage script, but the rings don’t disappear. This is the script that is not running after it’s parent (or in this case, grandparent) changes:


And this is the script that clones the ring:

Thank you for any help!

When you clone the parent, wait for the script and set enabled to false and instantly back to true.

I updated the hurt script, but the script in the ring (that flickers the transparency) still is not running.

I need more info, where is the script being placed into after being cloned? Send me a pic of your explorer or smth.

Okay, here is the explorer before playing:
Screenshot 2023-04-21 202320
This is where the hurt script (and the ring with the script) is located (after playing):


And this is another picture:

Edit: I’ve fixed the script.source for Instance.new("Script")

Yeah OP should either use this or put seperate scripts into each ring (totally the best way though)

I was going to ask if this was all in the same script. It gave me this error which I don’t understand: The current identity (2) cannot Source (lacking permission 1) - Client - Hurt:17
And also, I meant to write “rung” instead of “ring”.

Sorry, I made a mistake in the previous script.

Here is the updated one, which uses coroutine.create() instead of creating a new script.

-- Get the humanoid object
local humanoid = script.Parent.Parent:WaitForChild("Humanoid")

-- Define the function to generate rings
local function generateRings()
	local ring = script.ring:Clone()
	ring.Parent = script.Parent.Parent
	ring.CFrame = humanoid.RootPart.CFrame
	ring.Rotation = Vector3.new(math.random(1, 90), math.random(1, 90), math.random(1, 90))
	ring.Anchored = false
	ring.CanCollide = true
        
    --Create Script for Ring
	coroutine.resume(coroutine.create(function() 
		-- Wait for the parent and grandparent to load before running the script
		repeat wait() until ring:IsA("Model") --// Change if Ring IsA MeshPart or something

		-- Blink effect to simulate the ring disappearing
		for i = 1, 4 do
			ring.Transparency = 1
			wait(0.1)
			ring.Transparency = 0
			wait(0.1)
		end

		ring:Destroy()
	end)) 
	
end

-- Connect the Touched event to detect when the character is hit
humanoid.Touched:Connect(function(part)
	if part.Name == "Hurt" or part.Name == "hurt" then
		part.Name = "hirt"
		local playerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
		if playerGui.rings.Value > 0 then
			if playerGui.rings.Value <= 15 then
				for i = 1, playerGui.rings.Value do
					generateRings()
				end
			else
				for i = 1, 15 do
					generateRings()
				end
			end
		end
		playerGui.rings.Value = 0
	end
end)

I’ve never heard coroutine but the script works so it’s fine. Thank you!

1 Like

Why are you changing values in localscript?

1 Like

This is so that when the player gets hurt, they loose all of their rings.

Do it in a server-script then.

Why? Because the hurt script is a local script is StarterCharacterScripts and if I do it in a server script, it will remove the rings of every single player if one gets hurt.

You need to set the rings value to a special identifier like inside player folder or something. Anyways here is a better script handles most issues.

local Character:Model? = script.Parent
local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
local Humanoid:Humanoid? = Character:WaitForChild("Humanoid",25) or Character:FindFirstChildOfClass("Humanoid")
local function generateRings()
	local ring = script.ring:Clone()
	ring.Parent = workspace -- Parenting physics parts to character might cause some issues.
	ring.CFrame = Character.PrimaryPart.CFrame
	ring.Rotation = Vector3.new(math.random(1, 90), math.random(1, 90), math.random(1, 90))
	ring.Anchored = false
	ring.CanCollide = true

	--Create Script for Ring
	coroutine.wrap(function()
		-- Wait for the parent and grandparent to load before running the script
		repeat wait() until ring:IsA("Model") --// Change if Ring IsA MeshPart or something

		-- Blink effect to simulate the ring disappearing
		for i = 1, 4 do
			ring.Transparency = 1
			task.wait(0.1)
			ring.Transparency = 0
			wait(0.1)
		end
		ring:Destroy()
	end)()
end
Humanoid.Touched:Connect(function(Part)
	if tostring(Part):lower():match("hurt") then
		Part.Name = "hirt" -- ?
		local Rings:IntValue? = Player.Rings -- Rings!!
		if Rings.Value > 0 then
			if Rings.Value <= 15 then
				for i = 1, Rings.Value do
					generateRings()
				end
			else
				for i = 1, 15 do
					generateRings()
				end
			end
		end
		Rings.Value = 0
	end
end)
playerGui.rings.Value = 0 --// I've no idea why it's here, It was used by the original post

But there is no need because it already works fine in playergui.

It’s very exploitable in that case, a player might set its rings to inf or 0 to never lose them. heck, if they touch something that hurts they can briefly change them to 0 so they don’t lose it.

You can just use this.

local TextLabel:TextLabel? = script.Parent
local Player = game:GetService("Players").LocalPlayer
local Rings = Player.Rings
Rings.Changed:Connect(function(Value)
	TextLabel.Text = tostring(Value)
end)

And it’s there so that the ring text can update.

True, I might add an anti-cheat to check if the rings counter goes up more than 1. And if they get hurt when their rings are 0, they’re gonna get merked.