so i made a button gui when you would press it a model would attach on the player now i want the script to delete the item when the button gui is pressed again but i get this "blue underline" that says unknown global 'clonedvc'
here is the script
button = script.Parent.Parent.Parent local vc = game.ReplicatedStorage["Rig BP"] local plr = script.Parent.Parent.Parent local char = plr.character or plr.CharacterAdded:Wait() local attached = true button.MouseButton1Down:Connect(function() if attached == true then attached = false local Clonedvc = vc:Clone() Clonedvc.Parent = char Clonedvc:SetPrimaryPartCFrame(char:WaitForChild("UpperTorso").CFrame) Clonedvc:SetPrimaryPartCFrame(Clonedvc:GetPrimaryPartCFrame() * CFrame.new(0,-.84,.02) * CFrame.Angles(6.28,3.14,0)) local Weld = Instance.new("WeldConstraint") Weld.Part0 = Clonedvc.Main Weld.Part1 = char:WaitForChild("UpperTorso") Weld.Parent = char:WaitForChild("UpperTorso") print("Player is wearing model") elseif attached == false then attached = true Clonedvc.Parent = game.ReplicatedStorage end end)
look at line 22
This is a scope issue. A scope is a defined region where the program can recognize and use variables and functions declared within it. If an attempt is made to reference these things outside of its respective scope, you'll get an unknown global
exception—meaning it isn't valid in that space but where it was declared. To solve this issue, place the Clonedvc
declaration at a higher space within the .MouseButton1Down
signal:
button = script.Parent.Parent.Parent local vc = game.ReplicatedStorage["Rig BP"] local plr = script.Parent.Parent.Parent local char = plr.character or plr.CharacterAdded:Wait() local attached = true button.MouseButton1Down:Connect(function() local Clonedvc = vc:Clone() if attached == true then attached = false Clonedvc.Parent = char Clonedvc:SetPrimaryPartCFrame(char:WaitForChild("UpperTorso").CFrame) Clonedvc:SetPrimaryPartCFrame(Clonedvc:GetPrimaryPartCFrame() * CFrame.new(0,-.84,.02) * CFrame.Angles(6.28,3.14,0)) local Weld = Instance.new("WeldConstraint") Weld.Part0 = Clonedvc.Main Weld.Part1 = char:WaitForChild("UpperTorso") Weld.Parent = char:WaitForChild("UpperTorso") print("Player is wearing model") elseif attached == false then attached = true Clonedvc.Parent = game.ReplicatedStorage end end)
button = script.Parent.Parent.Parent local vc = game.ReplicatedStorage["Rig BP"] local plr = script.Parent.Parent.Parent local char = plr.character or plr.CharacterAdded:Wait() local attached = true button.MouseButton1Down:Connect(function() if attached == true then attached = false local Clonedvc = vc:Clone() Clonedvc.Parent = char Clonedvc:SetPrimaryPartCFrame(char:WaitForChild("UpperTorso").CFrame) Clonedvc:SetPrimaryPartCFrame(Clonedvc:GetPrimaryPartCFrame() * CFrame.new(0,-.84,.02) * CFrame.Angles(6.28,3.14,0)) local Weld = Instance.new("WeldConstraint") Weld.Part0 = Clonedvc.Main Weld.Part1 = char:WaitForChild("UpperTorso") Weld.Parent = char:WaitForChild("UpperTorso") print("Player is wearing model") elseif attached == false then attached = true vc:Destroy() -- or whatever u wanted detached just destroy that... end end)