Simple for loop Not Working

Ok this has to be an easy fix and I just do not know why it is not working. If anyone can help me and tell me why I am being so dumb right now I would love it. :sob:

--Give the player ingredients on click
local RS = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer

local backpack = player.Backpack

local ingredientsFolder = game.Workspace:WaitForChild("IngredientsGivers")
local potionModelFolder = RS:WaitForChild("PotionModels")

print("This works")

for _, ingredient in pairs(ingredientsFolder:GetChildren()) do
	local prompt = ingredient:WaitForChild("ProximityPrompt")
	prompt.Triggered:Connect(function()
		local clone = potionModelFolder:FindFirstChild(ingredient.Name):Clone()
		clone.Parent = backpack
		print("This works")
	end)
end

I don’t know what is wrong here. :sad: I am having a brain fart :sob:

I think you have to separate it into two scripts

Like a server and client script and connect them with a remote event? And if I did that it would be the client controling the tools right?

It could be that you are calling this before the backpack is parented to the Player, so instead of:

local backpack = player.Backpack

Try:

local backpack = player:WaitForChild("Backpack")

If I remember correctly, a new backpack is created everytime the character spawns/respawns.

If this works, I would also make a connection for when the character respawns to set the backpack variable to the new backpack.

1 Like

That did not work, I tried changing that and it did not work so then I tried this and it still does not work.

--Give the player ingredients on click
local RS = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer

local backpack = player:WaitForChild("Backpack")

local ingredientsFolder = game.Workspace:WaitForChild("IngredientsGivers")
local potionModelFolder = RS:WaitForChild("PotionModels")

print("This works")

for _, ingredient in pairs(ingredientsFolder:GetChildren()) do
	local prompt = ingredient:WaitForChild("ProximityPrompt")
	prompt.Triggered:Connect(function()
		print("This works")
		local partToClone = potionModelFolder:WaitForChild(ingredient.Name)
		local clone = partToClone:Clone()
		clone.Parent = backpack
	end)
end

My script is in StarterPlayerScripts if maybe that is what is wrong?

Nothing wrong with StarterPlayerScripts, as that is where most client scripts should be placed…

Are the names of all your proximity prompts actually called ProximityPrompt? Send a screenshot of the IngredientsGivers folder in the explorer.

1 Like

Here is the IngredientsGiver folder


And here is the PotionModels folder:

Just tested this myself, seems like it is a Client issue, but when ported to a Server script, it works fine.

Here is the version that you should put into a script in ServerScriptService, doing this on the server is more reliable and the Tool will replicate for other players.

--Give the player ingredients on click
local RS = game:GetService("ReplicatedStorage")

local ingredientsFolder = game.Workspace:FindFirstChild("IngredientsGivers")
local potionModelFolder = RS:FindFirstChild("PotionModels")

for _, ingredient in pairs(ingredientsFolder:GetChildren()) do
	local prompt = ingredient:FindFirstChild("ProximityPrompt")
	
	prompt.Triggered:Connect(function(Player)
		local backpack = Player:FindFirstChildWhichIsA("Backpack")
		if not backpack then return end
		
		local partToClone = potionModelFolder:FindFirstChild(ingredient.Name)
		if not partToClone then return end

		local clone = partToClone:Clone()
		clone.Parent = backpack
	end)
end
1 Like

Okay yeah, so I was able to get it also on the client it seems like I was trying to do everything before it all loaded in so I adjusted my script to this:

--Give the player ingredients on click
local RS = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local ingredientsFolder = game.Workspace:WaitForChild("IngredientsGivers")

if #ingredientsFolder:GetChildren() == 0 then
	ingredientsFolder.ChildAdded:Wait()
end

local potionModelFolder = RS:WaitForChild("PotionModels")
print("This works")
for _, ingredient in pairs(ingredientsFolder:GetChildren()) do
	local prompt = ingredient:WaitForChild("ProximityPrompt")
	prompt.Triggered:Connect(function()
		print("This works")
		local partToClone = potionModelFolder:WaitForChild(ingredient.Name)
		local clone = partToClone:Clone()
		clone.Parent = player.Backpack
	end)
end

And it worked. Which do you think would be better? Would the server option be better even if this works or is keeping track of tools on the client better?

Actually I just tested it and this is perfect, thank you! I realized as you said that others can not see the tool that I am holding if I do it all on the client and I do not want that. Thank you so much for your help!!!

1 Like

Whatever is best fit for your game, if its singleplayer then it doesn’t matter.

If multiplayer, then server just because of reliability and replication.

1 Like

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