Error - The Parent property of ClassicSword is locked

  1. What do you want to achieve? To clone a sword and place it in the player’s backpack per round and removing it once the round is over

  2. What is the issue? Once a new round starts this error appears

  3. What solutions have you tried so far? I have tried to look for solutions using the devforum but none have worked for me

The error appeears once the round has finished once and causes the rest of the script to stop indefinitely

--Variables
local lobbyPart = game.Workspace.SpawnLocation
local maps = game.ReplicatedStorage.Folder:GetChildren()
local status = game.ReplicatedStorage.Status
local inRound = game.ReplicatedStorage:WaitForChild("InRound")
local playersAlive = {}
local sword = game.ReplicatedStorage.ClassicSword:Clone()


local function choseMap(char, internval) -- chose map
	local mapsNo = math.random(1, #maps)
	local chosenMap = maps[mapsNo]:Clone()
	chosenMap.Parent = workspace
	print(chosenMap.Name)
	char.HumanoidRootPart.Position = chosenMap.TeleportPoint.Position

	if internval == 0 then
		task.wait(4)
		chosenMap:Destroy()
	end
end

local function round() -- start intermission and round
	for i, v in pairs(game.Players:GetPlayers()) do
		table.insert(playersAlive, i, v)
		if inRound == true then
			inRound = false
			break
		end
		local char = v.Character or v.CharacterAdded:Wait()
		if char.Humanoid then



			local hum = char.Humanoid

			for i = 30, 0, -1 do
				status.Value = "Intermission: "..i
				task.wait(1)
			end
			choseMap(char,i)
			status.Value = "Map Chosen!"
			task.wait(5)
			sword.Parent = v.Backpack
			for i = 120, 0, -1 do -- change i to 120 when done testing, 10 during testing


				hum.Died:Connect(function()
					table.remove(playersAlive, i)
					print(playersAlive)
					if #playersAlive == 1 then
						local winner = playersAlive[i]
						status.Value = "Round Ended"
						wait(5)
						inRound = true
						if v.Name == winner.Name then

							local leaderstats = playersAlive[i].leaderstats
							local coins = leaderstats.Coins
							local wins = leaderstats.Wins
							wins.Value = wins.Value + 1
							coins.Value = coins.Value + 200
							coins.Value = coins.Value
						else
							local leaderstats = v.leaderstats
							local coins = leaderstats.Coins
							coins.Value = coins.Value + 50
						end
					end

				end)
				status.Value = "Time Left: "..i
				if i == 0 then
					sword:Destroy()
					v.Character.HumanoidRootPart.Position = lobbyPart.Position
				end
				task.wait(1)
			end
		end
	end
end


while true do
	round()
	task.wait()
end

You only created the sword outside the round functions, meaning sword is global and is shared across functions.
The simple fix is to declare the cloned sword inside of the new round function. Basically, what you’re doing is:

local sword = ReplicatedStorage.Sword:Clone()
function round()
     sword.Parent = Player.Backpack
end
function endRound()
     sword:Destroy()
     -- This essentially means sword is now nil.
     -- If you run round() again, sword is nonexistent.
     -- ...Meaning you can't parent something nonexistent to another.
end

Again, “the simple fix is to declare the cloned sword inside of the new round function”:

function round()
     local sword = ReplicatedStorage.Sword:Clone()
end

Oh also, that’s a heavy memory leak right there… Why are you doing

while true do
	round()
	task.wait()
end

if I may ask?

you’re copying the sword then setting then parenting it and later deleting it and when a new round starts you are trying to parent that same copy which you just deleted. Instead copy the copy if that makes sense

I’m trying to run the function without having to deal with issues in certain parts running before others so having everything in a single function and have it run within a while true loop is the solution i came up with.To add to this i couldn’t come up with any other possible ways to attempt this as adding a 1 to the task.wait would cause the values to be updated every 2 seconds rather than 1

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