Parent Property Issue

Hey. I have had a lot of issues regarding the “Parent Property is locked”. I understand this happens when you destroy an object, and attempt to reparent it, however, I don’t see where the issue in my script is ( though it’s quite unorganized since I suck ).

The script is here;

-- Checking variables
local Models = game.ReplicatedStorage.ItemModels

local Flashlight = Models.FlashlightM:Clone()

local Player = game.Players.LocalPlayer
local Equipped = false
local UserInputService = game:GetService("UserInputService")
-- waiting for character

repeat wait() until Player.Character or Player.CharacterAdded:Wait()
local Character = Player.Character

-- Adding Flashlight

function Add_Item()
	local Weld = game.ReplicatedStorage.Flashlight:Clone()
	Flashlight.Parent = Character
	-- make sure its not anchored
	if Flashlight.Anchored == true then
		Flashlight.Anchored = false
	end
	Weld.Part0 = Character:WaitForChild("LowerTorso")
	Weld.Part1 = Flashlight
	Weld.Parent = Flashlight
end

-- Removing Flashlight

function Remove_Item()
	if Character:FindFirstChild("FlashlightM") then
		Character:FindFirstChild("FlashlightM"):Destroy()
	end
end

-- Making it visible in FPS

Flashlight.LocalTransparencyModifier = 0
Flashlight.Changed:Connect(function()
	Flashlight.LocalTransparencyModifier = Flashlight.Transparency	
end)

-- Flashlight Equip

UserInputService.InputBegan:Connect(function(Input, IsTyping)
	if Input.KeyCode == Enum.KeyCode.E and IsTyping == false and Equipped == false then
		Add_Item()
		local Gui = Player.PlayerGui.SpecialTools
		local Image = Gui.Flashlight.LightImage
		Image.ImageColor3 = Color3.new(255,0,0)
		wait(3)
		Equipped = true
	else
		if Equipped == true then
			Equipped = false
			local Gui = Player.PlayerGui.SpecialTools
			local Image = Gui.Flashlight.LightImage
			
			Remove_Item()
			Image.ImageColor3 = Color3.new(255,255,255)
			
		end
	end
	
end)

Legit everything functions, but once I equip the flashlight and unequip it, I can never do it again and I keep getting the error.

image

You destroyed the FlashlightM in the Remove_Item function. Also, when using Color3.new you have to use a number 0-1. So, you would put Color3.new(1, 1, 1) for white and Color3.new(1, 0, 0) for red.

it has to destroy it because when you unequip it, it destroys the flashlight.

color3 thing already works for me

Try putting:

elseif Input.KeyCode == Enum.KeyCode.E and IsTyping == false and Equipped == true then

instead of just the else. Also, remove the if Equipped == true then. Btw, why did you put a wait(3)? The script waits 3 seconds before saying that the flashlight is equipped.

Alright, let me try this rq in studio

Still does this: https://gyazo.com/9bf0d32eae4c1cec404c28f9f2feae13

The wait(3) is a makeshift cooldown for the flashlight ( so it doesn’t spam, also prevents exploiting from lagging out a server )

You have something in ReplicatedStorage called “Flashlight” and you named it weld. So, is it a weld or not because that might be causing the problem.

You can’t do anything with objects that you Destroy(). You could probably just set the parent of the flashlight to nil instead of destroying it. Also, your input logic is incorrect because the flashlight will get unequipped if any button other than E is pressed or IsTyping is true. It should look something like this:

InputBegan:Connect(function(Input, IsTyping)
    if Input.KeyCode == Enum.KeyCode.E and IsTyping == false then
        if Equipped == false then
            -- equip
        else
            -- unequip
        end
    end
end)

The color values for the Color3 are also incorrect, like @NeonTaco135 mentioned. If you want to use values 0-255 then you should use Color3.fromRGB(r, g, b)

Thanks a lot! Learned a lot from this reply, and it functions correctly.

Tysm. Have a great summer and day.