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

Pick-up Script written properly?

Asked by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Hello, I’m not in Studio and have no access to it at the moment, yet I decided to write a Pick-up Script to Grab a Rifle laying on the Ground, then place it on your Back, I would just like someone to let me know if there’s any potential issues, as well as helping me set the CFrames for the placement on the Back and Orientation

local Rifle = workspace:WaitForChild(“Rifle”)
local ClickDetector = Rifle:FindFirstChild(“ClickDetector”)
ClickDetector.MouseClick:Connect(function(Player)
   if (Player) then
      local Character = Player.Character or Player.CharacterAdded:wait()
      if (Character and Rifle) then
         local Torso = Character:FindFirstChild(“Torso”)
         if (Character and Rifle and Torso) then
            local Weld = Instance.new(“Weld”)
            Rifle.CFrame = Torso.CFrame * CFrame.new(Back Vector) * CFrame.Angles(Orientation Vector)
            Weld.Part0 = Torso
            Weld.C0 = Torso.CFrame:Inverse()
            Weld.Part1 = Rifle
            Weld.C1 = Rifle.CFrame:Inverse()
            Weld.Parent = Rifle
         end
      end
   end
end)

Thanks!

0
you dont need to check if rifle exists, because the script will yield if it doesnt. and you definitely dont need to check for it and character twice Gey4Jesus69 2705 — 5y
0
character follows the same logic as rifle btw Gey4Jesus69 2705 — 5y
0
I heard that’s the best method to Index the Character, as it’s secondary precaution reinforces the Index making sure you get the Character, as obviously that’s Important. I also check to make sure the Rifle didn’t disappear on me, as that would break the Weld, unless I should wrap it in a pcall? Ziffixture 6913 — 5y
0
No. The rifle should not disappear on you User#24403 69 — 5y
0
Hey, you never know it “should not”, doesn’t mean it can’t Ziffixture 6913 — 5y

Answer this question