Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is wrong with this welding script?

Asked by
Mystdar 352 Moderation Voter
9 years ago

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)
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. Discern 1007 — 9y
0
I would like to confirm that, LOCAL SCRIPTS DO NOT RUN IN WORKSPACE! Perci1 4988 — 9y
0
Where would I put it? in the player? How would I get it there? Mystdar 352 — 9y
0
The best place to place this script is in either StarterGui or StarterPack. Discern 1007 — 9y
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' Mystdar 352 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

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)

0
Tried, doesn't work, any advice? Mystdar 352 — 9y
0
Updated using wait method, if still doesn't work tell output. Hope this helps! ~marcoantoniosantos3 marcoantoniosantos3 200 — 9y
Ad

Answer this question