Model cloned from server script with a server script inside of it is not working?

I made a shop system but the only thing is when a the pet model is cloned in the player’s inventory the server script won’t work. The server script inside of the pet checks if the boolvalue in the model has been updated to fire the script the rest the script.
Screenshot 2022-06-26 233729

I got this script off youtube to make the pet pop up so I’m not sure if it works but I added the part to check if the equipped value is true or not.

local pet = script.Parent
local plr = script.Parent.Parent.Parent


function givePet (player)
	if script.Parent.Equipped.Value == true then
		local character = player.Character
		if character then
			local humRootPart = character.HumanoidRootPart

			local bodyPos = Instance.new("BodyPosition", pet)
			bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

			local bodyGyro = Instance.new("BodyGyro", pet)
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)


			pet.CanCollide = false

			while wait() do
				bodyPos.Position = humRootPart.Position + Vector3.new(4.5, -0.9, 1.5)
				bodyGyro.CFrame = humRootPart.CFrame
			end

		end
	end
end

script.Parent.Equipped.Changed:Connect(function(player)
	print("FiredPetEquip")
	if script.Parent.Equipped.Value == true then
		givePet(player)
		
	end
end)

If you need more detail let me know!

Try wrapping that in courtine.wrap() so it runs on a new thread and the rest of the script can keep running

1 Like
coroutine.wrap(function()
	while wait() do
		bodyPos.Position = humRootPart.Position + Vector3.new(4.5, -0.9, 1.5)
		bodyGyro.CFrame = humRootPart.CFrame
	end
end)()

This is how, just in case. Also, while you’re at it, read the documentation too, just to learn

2 Likes

You could also do task.spawn:


taks.spawn(function()
	while wait() do
		bodyPos.Position = humRootPart.Position + Vector3.new(4.5, -0.9, 1.5)
		bodyGyro.CFrame = humRootPart.CFrame
	end
end)

2 Likes

Additionally you should use task.wait() instead of wait()

So either

task.spawn(function()
	while task.wait() do
		bodyPos.Position = humRootPart.Position + Vector3.new(4.5, -0.9, 1.5)
		bodyGyro.CFrame = humRootPart.CFrame
	end
end)

or

coroutine.wrap(function()
	while task.wait() do
		bodyPos.Position = humRootPart.Position + Vector3.new(4.5, -0.9, 1.5)
		bodyGyro.CFrame = humRootPart.CFrame
	end
end)()

should work.
I don’t think there is a difference between the two in this situation, but I’m not 100% sure.

Also, read coroutine for more info on coroutines and task for more info on the task library!

1 Like

Thank you! The only thing is the script just isn’t firing at all that’s why I posted this. I redid the script but just had it in the workspace and it worked, but the issue is that the script isn’t firing or doing anything when it is cloned into the player after they purchase it.

The issue is the script isn’t firing and I don’t know why. Read what I said to chromatype

This helps but the issue is the script not firing. Look what I told Chromatype.

Theres a really cool way to check where is the problem in a script that’s not doing something. Put print(xyz)s after everything, like every if statement, after the function, etc. Make all of them have a different output, run it, and you’ll see where the problem is (where the print didn’t print).

And the reason all 3 of us tried modifying the loop is that usually, the reason why a script doesn’t run is that it’s stuck in a loop inside it.

Also, is the script disabled before cloning? If not, disable it. Then enable the clone after you put it in the player. Every script should be disabled before cloning. This could fix it
Hope it works

1 Like

Never mind it still dosent work. I made it so when you buy the item it will enable the scripts and that part works just the scripts dont work. I even tested this by adding a print in a script that gets enabled and it dosent print when it is enabled. I’m so confused why it isn’t working.

Could you show the part where you clone the script?

And if you put the print to the beginning of the script and it still didn’t print, I don’t know what could be the problem. It should work

1 Like

Heres the script to clone the item that was bought. If you need to see the rest of the script let me know.

local clone = ShopItems[ItemValue]:Clone()
clone.Parent = plr.ShopItemSave
clone.EnableScripts.Disabled = false

here is the item being cloned.
Screen Shot 2022-06-27 at 1.09.23 PM

Here is the item when it is cloned inside of the player.
Screen Shot 2022-06-27 at 1.10.41 PM

Also the EnableScripts part is to enable the other scripts inside of the player, but it isn’t even working when I put in print.

I cant see a problem here. it should work. Can I see the EnableScripts too? Maybe I can find something there

Also, I think it would be better to clone these things inside the player’s character or the backpack, not the Player instance itself. But if the other scripts work (the Codes and Gamepass and such), it should work too I guess

Everything else works so I’m not sure why it does it either. Inside the script it is just a print(“Enabled Worked”) so I could test to see if it works.

The issue is that server scripts do not run when they are a descendant of a player instance.


(via PlayerScripts)

Server scripts will only run if they are parented to ServerScriptService, Workspace, or a player’s Backpack

1 Like

Oh alright. I’ll fix it after I go to the gym! Thank you

1 Like