How to spawn local parts from a folder

so basically, im trying to make local parts by cloning parts from a folder using a local script
then, when you touch the items, it is destroyed, but only in your side

however, it doesnt seem to spawn at workspace, even when i added PlayerAdded and CharacterAdded

here is the script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local itemsFolder = replicatedStorage:WaitForChild("Items")
local items = itemsFolder:GetChildren()

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Connect(function(character)
		for i, part in ipairs(items) do
			local clientItems = part:Clone()
			clientItems.Parent = workspace.Items --this is the folder where the cloned items are supposed to go
			print("Local items have been added")
			
			if clientItems:IsA("BasePart") then
				local isTouched = clientItems:WaitForChild("IsTouched")
				isTouched = false
				clientItems.Touched:Connect(function(hit)
					if not isTouched then
						isTouched = true
						clientItems:Destroy()
					end
				end)
			end
			
		end
	end)
	
end)

pls help

everything looks in shape so far, the first thing i can ask is, have you added any prints to check if the code even runs as intended?

no let me check

(minimum characters)

i added a print line, but it didnt print it out

is this script a local script? or is it on the server?

its a local script on workspace

i’m not 100% sure on how localscripts work but theres 2 issues with this. 1) i’m not sure if localscripts run on the client if they’re not in the player (i have to look this up i do not definitively know) and 2) you’re running PlayerAdded in a local script, for a player to run that, they have to be in the game meaning that event is Binded after they’ve loaded in. PlayerAdded won’t be fired unless another player joins in

Can’t test right now, but my best guess is that you shouldn’t use game.Players.PlayerAdded as it will not fire on your side but it will fire when someone else joins. Remove that, and instead of player.CharacterAdded, use game.Players.LocalPlayer.CharacterAdded.

this still doesnt work, but i found another option, so its fine

i fixed the problem, all i did was delete the PlayerAdded and CharacterAdded part, then move the script from workspace to StarterPlayerScripts

2 Likes

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