Tool Clone Not Parenting to Correct Player

Hello, I’ve been working on a cafe game and my scripting is kind of terrible so I’m seeking for help. I have it so that when they put the tool over the block the script is connected to, it’ll change the name, and give them a new tool while deleting the previous. I’ve gotten that down, yet I don’t know how to make it so it’ll check for who is holding the item so they can place the next tool in the correct Backpack. As of right now the script will put it in the first person in the Player folder. I am thinking it has something to do with FindFirstChild or the local player, but as I said, I am a new scripter so I have no idea where to look/what to do. I have looked at a few devforum posts yet none of them have helped me. Here is the script I’ve made so far.

local SS = game:GetService("ServerStorage")
local Tool = SS:FindFirstChild("Cupcake Batter")
local Player = game:GetService("Players")


function tch(h) -- when the tool touches the brick
    if (h.Parent.Name == "Batter") then --if the tool's name is Batter
        h.Parent.Name = "Pouring..." -- then set the tool's name to Pouring
        wait(5)
        h.Parent:Destroy() --then destroy the tool
        Tool:Clone().Parent = Player:FindFirstChild('Backpack',5)

    end
end

script.Parent.Touched:connect(tch)

If the script is a LocalScript you change the player variable to
local Player = game:GetService("Players").LocalPlayer

The script is not a local script.

You are defining the player service not the actual player holding the tool. I’m not sure where your script is so my example is using “FindFirstChildAncestorWhichIsA()” to define the tool and player.

-- Services --
local SS = game:GetService("ServerStorage");

-- Variables --
local Player = script:FindFirstAncestorWhichIsA("Player"); -- This will search every parent of the script until it finds a player instance
local Tool = script:FindFirstAncestorWhichIsA("Tool"); -- same thing as above but looks for a tool
local Backpack = Player.Backpack;
local ToolToGive = SS:FindFirstChild("Cupcake Batter");

-- Functions --
function Touch(Hit) -- when the tool touches the brick
	if Hit.Parent and Hit.Parent.Name == "Batter" then -- if the tool's name is Batter
		if Player and Tool then
			Tool.Name = "Pouring..." -- then set the tool's name to Pouring
			wait(5);
			ToolToGive:Clone().Parent = Backpack;
			Tool:Destroy(); --then destroy the tool
		end;
	end;
end;

script.Parent.Touched:connect(Touch);

Hey! I tried out that version, and I can’t get the script to work. As I said I’m not really the brightest at scripting, so maybe I’m just missing something easily fixable. Which I’m sorry about if so.

What I am trying to do is make the batter (red) turn into a cupcake batter (blue) by destroying the tool in hand and replacing it with the cupcake batter. Before it is destroyed, I want to change the name of the Batter for the player to “Pouring…” to make it seem like it is pouring into the pan.

This should be accomplished when the tool touches the “Head” brick.

Using our current script, the pouring works but when the tool is duplicated to be given to the person who touched the “Head” brick, it goes to the first person in “Players” instead of the one who touched the “Head” brick to activate the script. Meaning, this will not work with multiple people. I believe we are unable to add it to a local script as everyone needs to see that a player is holding the Cupcake Batter, but I am not sure if a local script will effect this.

Hopefully this clarifies what I am trying to solve.

image

image

In the script I provided change

if Hit.Parent and Hit.Parent.Name == "Batter" then -- if the tool's name is Batter
to
if Hit.Name == "Head" and Hit.Parent.Name == "Cupcake Pan Pour" then

this isnt part of the problem but it might be the tiny major part of the problem, why are you using findfirstchild instead of waitforchild? theres a extremely likely chance that the cupcake batter has not loaded yet in the server storage

Hey! I went ahead and changed a few things, and was able to get it working. I tested it out myself and it does exactly what you described. Read through it carefully and if you have any questions, just let me know!

local ServerStorage = game:GetService("ServerStorage")
local CupcakeBatter = ServerStorage:WaitForChild("Cupcake Batter") 
local Players = game:GetService("Players")


function onTouch(part)
	local plr = Players:GetPlayerFromCharacter(part.Parent.Parent) 
	if plr and part.Parent.Name == "Batter" then 
		local plrBackPack = plr:FindFirstChild("Backpack")
		if plrBackPack then
			part.Parent.Name = "Pouring..." 
			wait(5)
			part.Parent:Destroy() 
			CupcakeBatter:Clone().Parent = plrBackPack
		end
	end
end

script.Parent.Touched:connect(onTouch)