Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

what does Unknown global 'Clonedvc' mean?

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

3 comments

0

i think if u want to destroyit why are u parenting it to replicated storage

0

the problem is after i put "elseif" the script doesn't recognize "clonedvc"

0

Clonedvc:Destroy(), script still doesn't recognize "Clonedvc"

2 answers

✓ Accepted answer

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)

3 comments

0

now im getting an error that says "MouseButton1Down is not a valid member of Player"

0

oh wait hold on.

0

I fixed up the script a bit by telling where the cloned model is and it worked!

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.