Help with writing and implementing a script

Hello, I already made a post on this topic today, but no one could help me especially, thanks to one person who made the spawn boxes, but this is not the point

I want to make a simulator (with my friend - a designer, builder), and we chose such a mechanic when you press E on the keyboard, you pick up a piece and sell, I have already done half of the mechanics of the game, but I require your help, I can’t do so when a player clicked on E, he selected a box, since there are random boxes that I put in ReplicatedStorage and made random

I repeat, I need to help with the script so that when a player approaches a random box, he presses on E and he had that box in his hands

In these screenshots I show you what I want to implement
box1
box3

I hope for your responsiveness and help.

1 Like

All parts contains it’s position, you can use it’s position to figure it out which box it is

First get the humanoid:
local char = player.Character or player.CharacterAdded:Wait()
local humanoidrootpart = char:WaitForChild(“HumanoidRootPart”)

Then take away the humanoid position from the box using magnitude

local magnitude = (humanoidrootpart.Position - box.Position).Magnitude

I would use an attachment point and create a part weld through that.

So, each key point on a character, has an attachment point, for example, grip points on each hand

Each of these carry CFrame positions. You can then create a weld which links the box to the Grip point.

You’d do this like so:


game:GetService("Players").PlayerAdded:Connect(function(plr)  -- This is just to get the player itself
    plr.CharacterAdded:Wait() -- Waits for the character
    local char = plr.Character -- Identifies the character
    local RightHand = char:FindFirstChild("RightHand") -- Identifies the Right Hand
    local RightHandGrip = RightHand.RightGripAttachment
   
    -- From here onwards is where you'd create the weld, you can wrap this in a function or insert it into your code

    local Weld = instance.new("Weld")
    Weld.Part0 = RightHand -- This realistically can be anything, but it will move with the right hand if you link it to the right hand
    Weld.Part1 = [BoxVariable] -- You need to identify the box at some point
    Weld.C0 = RightHandGrip.CFrame  -- This is just the CFrame position of the Grip attachment to determine the position 
    Weld.C1 = [BoxVariable].CFrame -- The CFrame position of the Box variable
    Weld.Parent = [BoxVariable] -- This then places the weld in the box



end

To drop the box, you just remove the weld

[BoxVariable].Weld:Destroy()

Let me know if theres any error in the code or errors in logic, I’m writing from memory and not testing in studio.

The logic itself of the script should make sense and should give you enough of an understanding to re-write or adapt this code to work for you.

1 Like

Oyyy thank you so much and have a great day!

1 Like