[This is my first post, so it might not make the most sense, so bare with me. please!]
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.
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.
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)
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)
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.
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.
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.