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

How can i make a player hold a part?

Asked by 4 years ago

i would like to know how can i make the player hold a part/item by pressing "E" just like half-life with the crowbar or gmod with the crowbar where the part is parallel to the player and where the camera moves the item goes with it.

0
You're gonna have to make a tool for every model and use userinputservice to detect the player pressing E, when E is pressed the model in workspace is removed and the player gets the tool. Robowon1 323 — 4y

2 answers

Log in to vote
0
Answered by
gunter5 17
4 years ago

Create a part and put it inside a tool, call the part Handle. Test the game and equip the tool. Your avatar should be holding the part. Now all that's left to do Is build the shape of the crowbar.

0
He's asking how to do it without a tool. DeceptiveCaster 3761 — 4y
Ad
Log in to vote
-1
Answered by
ryan32t 306 Moderation Voter
4 years ago
Edited 4 years ago

I would create a Folder to contain all the Item Parts. Put the Folder into Workspace. Create two Remote Events, "E", and "Q". Create a Local Script in StarterGui. Create a Server Script in Server Script Service. Create a Server Script inside of a specific Item Part. In the Local Script, I would have it :FireServer to the E event when I press E to pick up an item. Or :FireServer to the Q event when press Q to drop an item. Then I would have the Server Script in Server Script Service to exist, so when I fired either of the Remote Events. It would detect that and proceed to do what I called the Event to do. Then the Server Script in the Specific Item Part is existing so you can activate the tool once the part is equipped( also assuming every item has it's own unique function. so it requires it's own individual script in it's own Part.. Yes?)

Local Script Code:

local plr = game.Players.LocalPlayer -- local player
local mouse = plr:GetMouse() -- mouse

game:GetService("UserInputService").InputBegan:Connect(function(k,b) -- key input event
    local key = k.KeyCode -- keycode
    if b == false then -- checking if player is not typing in chat
        if key == Enum.KeyCode.E then -- checking if keycode is E
            game.ReplicatedStorage.E:FireServer(mouse.Target) -- firing remote event
        elseif key == Enum.KeyCode.Q then -- checking if keycode is Q
            if plr.Character:FindFirstChildOfClass("Tool") then -- checking for tool
                game.ReplicatedStorage.Q:FireServer() -- firing remote event
            end
        end
    end
end)

(ServerScriptService) Server Script Code:

game.ReplicatedStorage.E.OnServerEvent:Connect(function(plr,target)
    if target:IsDescendantOf(workspace.Folder) then -- checks if descendant
        if (plr.Character.HumanoidRootPart.Position - target.Position).magnitude < 6 then -- distance check
            local tool = Instance.new("Tool") -- create tool
            tool.Name = "Crowbar" -- tool name
            local handle = target:Clone() -- clone part as handle
            target:Destroy() -- destroy part
            handle.Parent = tool -- set clone parent as tool
            handle.Name = "Handle" -- name clone "handle"
            tool.Parent = plr.Character -- set tool parent to character
        end
    end
end)

game.ReplicatedStorage.Q.OnServerEvent:Connect(function(plr)
    if plr.Character:FindFirstChildOfClass("Tool") then -- checks for tool
        local tool = plr.Character:FindFirstChildOfClass("Tool") -- tool variable
        tool.Handle.Name = tool.Name -- name handle as tool name
        tool[tool.Name].Parent = workspace.Folder -- set parent of handle as folder
        workspace.Folder[tool.Name].Position = workspace.CurrentCamera.CFrame.Position - workspace.CurrentCamera.CFrame.LookVector * 5 -- handle position
        tool:Destroy() -- destroy tool
    end
end)

(Part) Server Script Code:

script.Parent.AncestryChanged:Connect(function(ancestor) -- ancestor changed event
    if ancestor:IsA"Tool" and ancestor.Parent:IsA"Model" and ancestor.Parent:FindFirstChild"Humanoid" and game.Players:GetPlayerFromCharacter(ancestor.Parent) then -- checks
        ancestor.Activated:Connect(function() -- tool activated event
            print("Crowbar was swinged") -- print
        end)
    end
end)

Answer this question