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

What is wrong with this welding script?

This localscript is supposed to weld something to a player, if a value is changed to true, and remove it if it is change to false.

This is a local script in the workspace

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Magic = player.MagicEquiped	 -- There is a Boolvalue in the player called MagicEquiped
local playername = player.Name 

	mouse.KeyDown:connect(function(key) -- This bit here doesn't even work
		if key == "q" then
			if Magic == true then
				Magic = false
			elseif Magic == false then
				Magic = true
			end
		end
	end)
	
Magic.Changed:connect(function() 
	if Magic.Value == true then
		local hit = game.Workspace[playername]
		local LeftArm = hit["Left Arm"]
		if hit:findFirstChild("Humanoid") ~= nil and LeftArm:findFirstChild("MyLeftArm") == nil then
			g = game.Lighting.MyLeftArm:Clone()
			g.Parent = LeftArm
			local C = g:GetChildren()
			for i=1, #C do
				if C[i].className == "Part" or C[i].ClassName == "UnionOperation" then
					local W = Instance.new("Weld")
					W.Part0 = g.Middle
					W.Part1 = C[i]
					local CJ = CFrame.new(g.Middle.Position)
					local C0 = g.Middle.CFrame:inverse()*CJ
					local C1 = C[i].CFrame:inverse()*CJ
					W.C0 = C0
					W.C1 = C1
					W.Parent = g.Middle
				end
					local Y = Instance.new("Weld")
					Y.Part0 = LeftArm
					Y.Part1 = g.Middle
					Y.C0 = CFrame.new(0, 0, 0.05) * CFrame.fromEulerAnglesXYZ(0, 0, 0)
					Y.Parent = Y.Part0
			end
		end
	
	
	elseif Magic.Value == false then
		g:remove()
	end	
end)

5 comments

1

I haven't even looked at the script yet, I just read your introduction. This might be the error: LocalScripts don't run in Workspace.

0

I would like to confirm that, LOCAL SCRIPTS DO NOT RUN IN WORKSPACE!

0

Where would I put it? in the player? How would I get it there?

0

The best place to place this script is in either StarterGui or StarterPack.

0

It says that MagicEquiped is not a valid member of player, however when I look in the player, there is a boolvalue called 'MagicEquiped'

1 answer

Udated: Oops, missed wait(1) method!

I might solve your code, but first, remember that LOCALSCRIPTS actually work in Workspace. I made a game called Pepsi Escape, it has localscripts in workspace and they work perfectly.

For best results try putting it into StarterPack: Anyway... Here is my fixed code. You missed 'WaitForChild()' function.

But what is WaitForChild()??? WaitForChild is a method that freezes the whole script until the variable (object) gets added.

wait(1) -- I added the wait method to freeze the full script for 1 second, so it detects boolvalue when added (0.999999(...) seconds to be added).

local player = game:service("Players").LocalPlayer
local mouse = player:GetMouse()
local Magic = player:WaitForChild('MagicEquiped')    -- There is a Boolvalue in the player called MagicEquiped
local playername = player.Name 

    mouse.KeyDown:connect(function(key) -- This bit here doesn't even work
        if key == "q" then
            if Magic == true then
                Magic = false
            elseif Magic == false then
                Magic = true
            end
        end
    end)

Magic.Changed:connect(function() 
    if Magic.Value == true then
        local hit = game.Workspace[playername]
        local LeftArm = hit["Left Arm"]
        if hit:findFirstChild("Humanoid") ~= nil and LeftArm:findFirstChild("MyLeftArm") == nil then
            g = game.Lighting.MyLeftArm:Clone()
            g.Parent = LeftArm
            local C = g:GetChildren()
            for i=1, #C do
                if C[i].className == "Part" or C[i].ClassName == "UnionOperation" then
                    local W = Instance.new("Weld")
                    W.Part0 = g.Middle
                    W.Part1 = C[i]
                    local CJ = CFrame.new(g.Middle.Position)
                    local C0 = g.Middle.CFrame:inverse()*CJ
                    local C1 = C[i].CFrame:inverse()*CJ
                    W.C0 = C0
                    W.C1 = C1
                    W.Parent = g.Middle
                end
                    local Y = Instance.new("Weld")
                    Y.Part0 = LeftArm
                    Y.Part1 = g.Middle
                    Y.C0 = CFrame.new(0, 0, 0.05) * CFrame.fromEulerAnglesXYZ(0, 0, 0)
                    Y.Parent = Y.Part0
            end
        end


    elseif Magic.Value == false then
        g:remove()
    end 
end)

2 comments

0

Tried, doesn't work, any advice?

0

Updated using wait method, if still doesn't work tell output. Hope this helps! ~marcoantoniosantos3

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