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

How to get a weapon or any equipment by touching a part?

Asked by 9 years ago

I am sort of new to scripting on roblox and sorry if this is too "noobish" for you guys but I was wondering how would I make it so when I touch a part it gets me a weapon or any equipment into my hotkeys.

1 answer

Log in to vote
2
Answered by 9 years ago

You can use the Touched() event, the Clone() method, and the GetPlayerFromCharacter() method. The touched event calls a function whenever a specified part is touched, and it passes whatever the part touches as the first argument to the specified function. The GetPlayerFromCharacter method accepts a character as an argument, and finds the corresponding player. The clone method creates a clone carrying the properties of whatever instance it is used on. In this case, we clone the tool into the player's backpack whenever the part is touched and if we can not find the tool inside the player already.

--Assuming the script is in the part
--Assuming the tool is named "tool" and is located in ServerStorage

local part = script.Parent--references the part
local tool = game.ServerStorage:FindFirstChild("tool")--references the tool

part.Touched:connect(function(hit) --Anonymous function called when the part is touched, where "hit" is whatever touched the part
local character = hit.Parent--player's character
local player = game.Players:GetPlayerFromCharacter(character)

if player and hit:IsA("BasePart") and not (player.Backpack:FindFirstChild("tool") and character:FindFirstChild("tool")) then--If the player is found, "hit" is a part and the tool cannot be found in the player's character or backpack, then end then execute the following code
local clone = tool:Clone()--clones the tool
tool.Parent = player.Backpack--puts the clone into the player's backpack
end--closes the if statement
end)--closes the Touched() event
Ad

Answer this question