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

Can anyone help me position my weld script correctly?

Asked by
sad_eyez 162
6 years ago
Edited 6 years ago

Hello, so there is no error, because everything works perfect, but the thing is, I have not really ever used welds before, and I am needing some help positioning the CFrame of the weld in the correct position, I am making a pause menu type of thing, and I want the player to be able to hold the phone like normal, can anyone help me with the positioning?

Code:

local uis = game:GetService("UserInputService")


local plr = game.Players.LocalPlayer
local music = plr.PlayerGui:WaitForChild("MenuMusic")
local phonegui = plr.PlayerGui:WaitForChild("PhoneGui")
local char = plr.Character or plr.CharacterAdded:wait()
local hum = char:WaitForChild("Humanoid")
local hand = char:WaitForChild("LeftHand")

local cam = workspace.CurrentCamera

local phone = game:GetService("ReplicatedStorage")["Phone"]


local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://1212301570"

local animTrack = hum:LoadAnimation(anim)

local holding = false
local spamprotect = false

uis.InputBegan:Connect(function(input, gp)
    if (gp) then
        return
    else
        if (input.UserInputType == Enum.UserInputType.Keyboard) then
            if (input.KeyCode == Enum.KeyCode.P) and (holding == false) and (spamprotect == false) then
                spamprotect = true

                for i,v in pairs(char:GetChildren()) do
                    if v:IsA("Accessory") then
                        for i,x in pairs(v:GetChildren()) do
                            if x:IsA("Part") then
                                x.Transparency = 1
                            end
                        end
                    end
                end

                local clonedPhone = phone:Clone()
                clonedPhone.Parent = char
                clonedPhone.Position = char:WaitForChild("LeftHand").Position
                clonedPhone.CFrame = clonedPhone.CFrame * CFrame.fromEulerAnglesXYZ(0,5,0)
                local weld = Instance.new("Weld")
                weld.Part0 = hand
                weld.C0 = hand.CFrame
                weld.Part1 = clonedPhone
                weld.C1 = CFrame.fromEulerAnglesXYZ(89.6,0,-0.65) *hand.CFrame * CFrame.new(0,0,0)
                weld.Parent = clonedPhone   

                holding = true
                hum.WalkSpeed = 0
                hum.JumpPower = 0
                hum.MaxHealth = math.huge
                hum.Health = hum.MaxHealth
                animTrack:Play()
                wait(0.2)
                cam.CameraType = Enum.CameraType.Scriptable
                cam.CameraSubject = clonedPhone
                cam.CoordinateFrame = CFrame.new(char:WaitForChild("Head").Position, clonedPhone.Position) * CFrame.fromEulerAnglesXYZ(0,0,0)
                cam.FieldOfView = 50

                local blur = Instance.new("BlurEffect", cam)
                blur.Name = "Blur"
                blur.Size = 0
                for i = 0,5 do
                    wait(0.05)
                    blur.Size = blur.Size +1
                end

                phonegui.Adornee = clonedPhone
                music:Play()

                spamprotect = false

            elseif (input.KeyCode == Enum.KeyCode.P) and (holding == true) and (spamprotect == false) then

                spamprotect = true

                cam:FindFirstChild("Blur"):Destroy()
                char:FindFirstChild("Phone"):Destroy()
                holding = false
                animTrack:Stop()
                cam.CameraType = Enum.CameraType.Custom
                cam.CameraSubject = hum
                cam.FieldOfView = 70
                hum.WalkSpeed = 16
                hum.JumpPower = 50
                hum.MaxHealth = 100
                hum.Health = hum.MaxHealth

                music:Stop()

                for i,v in pairs(char:GetChildren()) do
                    if v:IsA("Accessory") then
                        for i,x in pairs(v:GetChildren()) do
                            if x:IsA("Part") then
                                x.Transparency = 0
                            end
                        end
                    end
                end

                spamprotect = false

            end
        end
    end
end)

The weld part is all you need to worry about, I am just pasting the whole script in case you need to test it in studio if you are able to help me.

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Here's what I do to weld:

local Weld = Instance.new("Weld")
Weld.Part0 = MainPart
Weld.C0 = MainPart.CFrame:inverse()
Weld.Part1 =  Object
Weld.C1 = Object.CFrame:inverse()
Weld.Name = Object.Name
Weld.Parent = MainPart

Basically you inverse the CFrame of the part in the weld ; Wiki:CFrame#inverse
(inverse means opposite in other words)
also: Don't use instance parent parameter
If this helps please accept :)

Ad

Answer this question