How do i clone a value that has the same name as the part touched and put it into a different folder?

Hello! I’m making those “Find the _____” game and i’m trying to make a script where when you touch a melon in workspace, it takes the value that has the same name of the melon in workspace and put it into a folder under that player…

I can’t seem to acheive that as this script ran through the printed “TIMBERRRRRR” but it doesn’t clone the value.

Please let me know if you have a solution!

image
image

local melonsFolder = script.Parent
local replicatedStorage = game:GetService("ReplicatedStorage")
local melonValuesFolder = replicatedStorage:FindFirstChild("MelonValues")
local Players = game:GetService("Players")

-- Function to handle the touch event
local function onPartTouched(other)
    -- Check if the thing that touched the part is a player's character
    local player = game.Players:GetPlayerFromCharacter(other.Parent)
    if player then
        -- Get the name of the part that was touched
        local touchedPartName = other.Name
        -- Find the corresponding value in the MelonValues folder
        local valueToClone = melonValuesFolder:FindFirstChild(touchedPartName)
        if valueToClone then
			-- Clone the value and parent it to the target folder
		Players.PlayerAdded:Connect(function(player)
			local targetFolder = player:WaitForChild("melonsCollected")
			local clonedValue = valueToClone:Clone()
			clonedValue.Parent = targetFolder
		end)
        end
    end
end

-- Connect the touch event to all parts in the Melons folder
for _, part in ipairs(melonsFolder:GetChildren()) do
    if part:IsA("BasePart") then -- Ensure the instance is a part
        part.Touched:Connect(onPartTouched)
    end
end

print("TIMBERRRRRRRR")

Possibly because your only adding the value when a new player is added. I believe you don’t need to add the “Connect”

Players.PlayerAdded:Connect(function(player)
	local targetFolder = player:WaitForChild("melonsCollected")
	local clonedValue = valueToClone:Clone()
	clonedValue.Parent = targetFolder
end)

hmmm it doesnt work still…

		Players.PlayerAdded(function(player)
			local targetFolder = player:WaitForChild("melonsCollected")
			local clonedValue = valueToClone:Clone()
			clonedValue.Parent = targetFolder
		end)

i used another way but it don’t seem to work as well

			local targetFolder = game.Players:WaitForChild("melonsCollected")
			local clonedValue = valueToClone:Clone()
			clonedValue.Parent = targetFolder

You really don’t need the PlayerAdded function when you already have the player anyways, and it isn’t like the player changes. @DiscipIin3 was on the right track, but it’s just better to remove the function entirely, as I said before, it is not needed.

local targetFolder = player:WaitForChild("melonsCollected")
local clonedValue = valueToClone:Clone()
clonedValue.Parent = targetFolder

That doesn’t seem to work, I was trying to get the folder like this
image

What do you mean by this exactly? Do you mean my code doesn’t work, or you tested it, and it didn’t work? When you said “I was trying to get the folder like this”, I got a little confused on what you meant there.

My apologies, the code didn’t clone the values and parent it into the player’s folder.

Alright, thanks for the clarification. Try this code instead. This is only for debugging the issue, so tell me if both of them print, and especially tell me if player is nil.

print(player)

local targetFolder = player:WaitForChild("melonsCollected")
local clonedValue = valueToClone:Clone()
clonedValue.Parent = targetFolder

print("works")

Ah, it seems like it does not print those 2 prints you sent after I touched the items

Why do you need the name of the part that touched you when cloning the value? Don’t you grab the name of the melon part?

Oof, how would I fix that? That’s really weird, I didn’t realize…

Try this code. It pretty simple to fix, actually. Just send the melonPart when you activate the function, and get the name of that instead.

local function onPartTouched(other, melonPart)
	-- Check if the thing that touched the part is a player's character
	local player = game.Players:GetPlayerFromCharacter(other.Parent)
	if player then
		-- Get the name of the part that was touched
		local touchedPartName = melonPart.Name
		-- Find the corresponding value in the MelonValues folder
		local valueToClone = melonValuesFolder:FindFirstChild(touchedPartName)
		if valueToClone then
			-- Clone the value and parent it to the target folder
			Players.PlayerAdded:Connect(function(player)
				local targetFolder = player:WaitForChild("melonsCollected")
				local clonedValue = valueToClone:Clone()
				clonedValue.Parent = targetFolder
			end)
		end
	end
end

-- Connect the touch event to all parts in the Melons folder
for _, part in ipairs(melonsFolder:GetChildren()) do
	if part:IsA("BasePart") then -- Ensure the instance is a part
		part.Touched:Connect(function(other)
			onPartTouched(other, part)
		end)
	end
end

Ah, thank you so much! It worked! I really appreciate it! Another request, is there anyway i can make it clone only once and it won’t clone like a bunch of them at a time?

Sure! After cloning, just return, and it should stop:

local targetFolder = player:WaitForChild("melonsCollected")
local clonedValue = valueToClone:Clone()
clonedValue.Parent = targetFolder

return

It still seem to clone multiple

local function onPartTouched(other, melonPart)
	-- Check if the thing that touched the part is a player's character
	local player = game.Players:GetPlayerFromCharacter(other.Parent)
	if player then
		-- Get the name of the part that was touched
		local touchedPartName = melonPart.Name
		-- Find the corresponding value in the MelonValues folder
		local valueToClone = melonValuesFolder:FindFirstChild(touchedPartName)
		if valueToClone then
			-- Clone the value and parent it to the target folder
				local targetFolder = player:WaitForChild("melonsCollected")
				local clonedValue = valueToClone:Clone()
				clonedValue.Parent = targetFolder
				clonedValue.Value = true
				return
			end
				
		end
	end

Oh yeah, right. Try adding this to the very front of the function (emphasis on “very front”). This should not add clones:

if player:WaitForChild("melonsCollected"):FindFirstChild(melonPart.Name) then return end

Works well! Tysm for everything. You’re a life saver ! :slight_smile:

1 Like

Oof, sorry, when I meant by Connect I mean like the entire connection function…

just try melonPart.CanTouch = false at the start of the function

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.