Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can i make a player hold a part?

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.

1 comment

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.

2 answers

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.

1 comment

0

He's asking how to do it without a tool.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.