Trying to Set Locked Parent

I keep getting a trying to set locked parent error in this script please help thanks.

local ballTool = game.ReplicatedStorage.Dodgeball:Clone()

local me = script.Parent

me.Touched:Connect(function(hit)
	
	if hit.Parent:FindFirstChild("Humanoid") then
		local hasBall = game.Players:GetPlayerFromCharacter(hit.Parent).IMFOLDER.hasBall.Value
		if hasBall == false then
			 ballTool.Parent = game.Players:GetPlayerFromCharacter(hit.Parent).Backpack
		hit.Parent.Humanoid:EquipTool(ballTool)

		end
		
	end
end)
1 Like

Well if the UI has been :Remove or :Destroyed, you might get this error.
Make sure the instance is still valid when you try to do this.

It seems that everything is valid though unless im just overlooking something? is there anything else that causes this error? Because the ball from rep storage is going to the player who touched it backpack so

Could you send a screenshot of the error? Also what line?


It only does this when there is more than 1 dodgeball also it isn’t showing what line it is on and the error occurs when the second ball is picked up.

1 Like

Not too sure about this since I’m on mobile now. But I made a guess.

Did you set the value of hasBall to true after the Player picked the ball? Else it will continue the loop forever. Also instead of doing if hasBall == false you can just do if not hasBall

Another thing that might be causing the bug is letting the player equip the tool right after parenting it to his backpack. Instead you can just parent the tool directly to the player’s character, which will do the same.

Bonus: It’s preferred to store the results of Players:GetPlayerFromCharacter() in a variable.

Hope this helps, or if it fails I will follow up later today.

2 Likes

That error usually occurs on destroyed parts. When Destroy() is called on a part, it is parented to nil and the parent is locked. However, I have not seen the error you are getting (usually, it says, thing tried to set locked parent of thing to thing, current parent is thing, something along those lines, and I think it’s an error and not a warning)
It’s likely caused by Humanoid:EquipTool() (which also might be why the error is different), I think it acts weird when trying to equip it when it already is? I ran into a weird bug once that occurred when picking up a tool from the ground, and I think the fix was to check if the parent of the tool was the character, and if it was, to not call EquipTool()

You are also parenting it to the packback then using EquipTool which is not ideal

1 Like

It seems that even without the script and using two dodgeball tools (which are automatically picked up) I still get the same error and the ball throwing script doesn’t work right (it throws it from the wrong location towards the mouse)

It seems that even without the script and using two dodgeball tools (which are automatically picked up) I still get the same error and the ball throwing script doesn’t work right (it throws it from the wrong location towards the mouse). I would rather try to do this without the script at this point.

This script may be part of my problem?

--local script

local remoteEvent = game.ReplicatedStorage.Throw
local BallRemoteEvent = game.ReplicatedStorage.SpawnNewBall

local tool = script.Parent

local cam = workspace.CurrentCamera
local maxZoom = 25
local minZoom = 50

local plrStrength = game.Players.LocalPlayer.IMFOLDER.PlayerStrength.Value
local ogplrStrength = plrStrength -- Does not change because it is the strength that zooming out reverts to

local uis = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local plrPos = player.Character.HumanoidRootPart.CFrame

local mousePos = player:GetMouse().Hit.p

local throwAnim =  script:WaitForChild("Throwing")
local thowingAnim =  player.Character.Humanoid:LoadAnimation(throwAnim)

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://15540379440" 

local animationTrack = player.Character.Humanoid:LoadAnimation(animation)




-- TOOL IS USED HERE

tool.Activated:Connect(function()
	mousePos = player:GetMouse().Hit.p
	remoteEvent:FireServer(mousePos, plrStrength)
	game.Players.LocalPlayer.IMFOLDER.hasBall.Value = false
	print(game.Players.LocalPlayer.IMFOLDER.hasBall.Value)
	tool:Destroy()
	animationTrack:Stop()
	BallRemoteEvent:FireServer()
	repeat wait()
		cam.FieldOfView += 1
	until cam.FieldOfView >= minZoom
end)

--TOOL IS GRABBED HERE

tool.Equipped:Connect(function()
	local userInputService = game:GetService("UserInputService")
	local pressed = false
	game.Players.LocalPlayer.IMFOLDER.hasBall.Value = true
	print(game.Players.LocalPlayer.IMFOLDER.hasBall.Value)
	
	--ZOOMING IS STARTED

	userInputService.InputBegan:Connect(function(input, isTyping)
		if not isTyping then
			if input.UserInputType== Enum.UserInputType.MouseButton2 then
				if not pressed then
					pressed = true
					animationTrack:Play()
					repeat wait()
						cam.FieldOfView -= 1
						plrStrength += 2


					until not pressed or cam.FieldOfView <= maxZoom

				end
			end
		end
	end)

	--ZOOMING IS STOPPED

	userInputService.InputEnded:Connect(function(input, isTyping)
		if not isTyping then
			if input.UserInputType == Enum.UserInputType.MouseButton2 then
				pressed = false
				animationTrack:Stop()
				repeat wait()
					cam.FieldOfView += 1
					plrStrength = ogplrStrength
				until pressed or cam.FieldOfView >= minZoom
			end
		end
	end)

end)




I found the problem here. When it destroys the tool I get this error so instead I changed it to set the parent into Replicated Storage like this

tool.Parent = game.ReplicatedStorage

This works temporarily but if anyone has any suggestions on how I can remove the ball from the game that would be helpful thanks