[First Post] Cloning Models does not work properly

[This is my first post, so it might not make the most sense, so bare with me. please!]

  1. What do I want to achieve? So I made a game and I wanted to have a player click a button, and a Chicken is supposed to be cloned and appear in the player’s plot and follow a path to a farm and be destroyed and then add Money to the players leaderstats.

  2. What is the issue? The issue with this is when there are 2 players in game(It works fine with one player), and one of the players clicks the button to spawn a chicken the Chicken spawns on the other players plot and it does not give the player that clicked the button the specified amount of money that the Chicken is supposed to give.

  3. What solutions have you tried so far? I looked all over the Forum and could not find anything, I looked on Youtube and still could not find any thing. I tried remaking the system and this would be my third time making it, soo. I don’t know what to do other than going and making a topic on the forum.

Here is some code:

-- Im not the best at commenting code. 

--Local script inside of the button that spawns the Chicken
local Button = script.Parent
local Player = game:GetService("Players").LocalPlayer

local Chicken = game.ReplicatedStorage.Chicken

Button.MouseButton1Click:Connect(function()
	local ClonedChicken = Chicken:Clone()
	ClonedChicken.Name = "ClonedChicken"
	ClonedChicken.PrimaryPart = ClonedChicken.HitBox
	for i, plt in pairs(workspace.plots:GetChildren()) do 
		if plt.owner.Value == Player.Name then
			ClonedChicken.Parent = plt --Assigns the Cloned Chicken to the plot that the player has.
			break
		end
	end	

	while true do 
		for i = 1, 13 do 
			wait()
			ClonedChicken:SetPrimaryPartCFrame(CFrame.new(ClonedChicken.PrimaryPart.Position - Vector3.new(0,0.5,1)))
		end
		for i = 1, 100 do 
			wait()	
			ClonedChicken:SetPrimaryPartCFrame(CFrame.new(ClonedChicken.PrimaryPart.Position - Vector3.new(0,0,1)))
		end
		ClonedChicken.PrimaryPart.Orientation = Vector3.new(0,90,0)
		for i = 1, 50 do 
			wait()
			ClonedChicken:SetPrimaryPartCFrame(CFrame.new(ClonedChicken.PrimaryPart.Position + Vector3.new(1,0,0)))
		end
		break
	end
	ClonedChicken.Parent = game.ReplicatedStorage
	Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + ClonedChicken.Multiplier.Value
	ClonedChicken:Destroy()
end)

May I ask if this script is in a local script? If so the other wouldn’t be able to see it.

Yes, this is in a local script.

Yep then only one player can see it. Maybe try changing this to a script instead?

-- Im not the best at commenting code. 

--script inside of the button that spawns the Chicken
local Button = script.Parent
local Player = script:FindFirstAncestorOfClass("Player") -- Work the same way of finding a local player through a local script located in playergui

local Chicken = game.ReplicatedStorage.Chicken

Button.MouseButton1Click:Connect(function()
	local ClonedChicken = Chicken:Clone()
	ClonedChicken.Name = "ClonedChicken"
	ClonedChicken.PrimaryPart = ClonedChicken.HitBox
	for i, plt in pairs(workspace.plots:GetChildren()) do 
		if plt.owner.Value == Player.Name then
			ClonedChicken.Parent = plt --Assigns the Cloned Chicken to the plot that the player has.
			break
		end
	end	

	while true do 
		for i = 1, 13 do 
			wait()
			ClonedChicken:SetPrimaryPartCFrame(CFrame.new(ClonedChicken.PrimaryPart.Position - Vector3.new(0,0.5,1)))
		end
		for i = 1, 100 do 
			wait()	
			ClonedChicken:SetPrimaryPartCFrame(CFrame.new(ClonedChicken.PrimaryPart.Position - Vector3.new(0,0,1)))
		end
		ClonedChicken.PrimaryPart.Orientation = Vector3.new(0,90,0)
		for i = 1, 50 do 
			wait()
			ClonedChicken:SetPrimaryPartCFrame(CFrame.new(ClonedChicken.PrimaryPart.Position + Vector3.new(1,0,0)))
		end
		break
	end
	ClonedChicken.Parent = game.ReplicatedStorage
	Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + ClonedChicken.Multiplier.Value
	ClonedChicken:Destroy()
end)
2 Likes

Any errors? Also you move the Chicken to the plot. I would recommend each plot having a part named “Chicken Spawn”), then when you parent the Chicken to the pot, move the Chicken to the "Chicken Spawn’s CFrame.

MouseButton1Click does not work in Server Scripts. When they press the button use a remote event to tell the server, then run the rest of the code on the server.

It does work in a script but not commonly used.
Screen Shot 2564-01-18 at 08.07.49


So what is recommended? Firing a remote event in a local script and having the listener in like the workspace and have it spawn the chicken?

Personally I’d go for the remote if you want a smooth transit and non laggy look but it can easily be exploited so I’d just go with my method since FireAllClients does exist.

If you’re using a localscript to get the localPlayer you can do

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

depending on where your gui is located. (In a serverscript) because the gui will be cloned to the player’s player gui and if you locate the player gui’s parent you can get the player.

So, what does FindFirstAncestorOfClass() do? Since I’m not the best programmer

I’m not that good at explaining things but I’ll try. Basically it’ll return the parent of that class that you’re looking for, an example is that I wanted it to find a player class in a local script or script located in PlayerGui.

1 Like

Here’s what it looks like, I might be wrong though.

Screen Shot 2564-01-18 at 08.22.53

1 Like

Ohhh, ok that makes more since, thank you.

1 Like

If you want me to take screen shots to help you help me? I will gladly do so.

Sure, but I’ll dm you instead so it doesn’t flooded the chat here lol