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

how to find a humanoid in starterpack scripts?

Asked by 2 years ago
Edited 2 years ago

i was trying to do a script but theres a problem. where do i find the humanoid for example This is the script:

local tool = script.Parent

function spray(plr)

    local handle = game.StarterPack.Tool.Handle
    local humanoid = workspace:FindFirstChild("Humanoid")

local new_part = Instance.new("Part", workspace)
new_part.Name = "Spray"
    new_part.Position = workspace[humanoid.Parent.Name].Tool.Handle.Position
    new_part.Size = Vector3.new(7,2,7)
    new_part.Color = Color3.new(0.0470588, 0.635294, 1)
    new_part.Transparency = 0.8
    new_part.Anchored = false

    wait(6)

    new_part:Destroy()

end

tool.Activated:Connect(spray)

function(hit), hit.Parent:FindFirstChild, im just confused.

1 answer

Log in to vote
0
Answered by
Rinpix 639 Moderation Voter
2 years ago

If you want to access the humanoid of the player that has the tool, do this at the beginning of the script:

local tool = script.Parent
local player = tool.Parent.Parent
local character = player.Character
local humanoid = character.Humanoid

Also, this line:

local handle = game.StarterPack.Tool.Handle

won't work at all. You'd be accessing the handle of the tool that is being stored in the StarterPack service, not the tool that this player has. If you're trying to get the handle of this instance of the tool, just do this:

local handle = tool.Handle

As for this line:

local humanoid = workspace:FindFirstChild("Humanoid")

You're not going to find a humanoid in the workspace. They're always going to be inside of characters, which are in the workspace. Yes, it is technically in the workspace, but inside of something else in the workspace. It's not a direct child, it's a descendant. Besides, even if you did this:

local humanoid = workspace:FindFirstDescendantWhichIsA("Humanoid")

You still wouldn't be ensuring that you're accessing your humanoid; it could be finding another player's humanoid first.

The key to accessing your character is the tool itself.

Right here, you parented a new part to the workspace and then modified its properties.

local new_part = Instance.new("Part", workspace)
new_part.Name = "Spray"
new_part.Position = workspace[humanoid.Parent.Name].Tool.Handle.Position
new_part.Size = Vector3.new(7,2,7)
new_part.Color = Color3.new(0.0470588, 0.635294, 1)
new_part.Transparency = 0.8
new_part.Anchored = false

What you should be doing is instantiating it, modifying its properties, and then parenting it to the workspace. If you parent it and then modify it, you'd risk letting players see it in its default state. That bit of code should look like this:

local new_part = Instance.new("Part")
new_part.Name = "Spray"
new_part.Position = workspace[humanoid.Parent.Name].Tool.Handle.Position
new_part.Size = Vector3.new(7,2,7)
new_part.Color = Color3.new(0.0470588, 0.635294, 1)
new_part.Transparency = 0.8
new_part.Anchored = false

new_part.Parent = game.Workspace

Well, not entirely. This:

new_part.Position = workspace[humanoid.Parent.Name].Tool.Handle.Position

is redundant. You already defined a variable for the handle, so typing all of that just defeats the whole purpose of creating that variable. Do this instead:

new_part.Position = handle.Position

Additionally, doing this:

new_part.Anchored = false

is also redundant. Parts are unanchored by default.

So, the bit of code where you're creating the part and changing it should look like this:

local new_part = Instance.new("Part")
new_part.Name = "Spray"
new_part.Position = handle.Position
new_part.Size = Vector3.new(7,2,7)
new_part.Color = Color3.new(0.0470588, 0.635294, 1)
new_part.Transparency = 0.8

new_part.Parent = game.Workspace

All of that considered, the final script should look like this:

local tool = script.Parent
local handle = tool.Handle
local player = tool.Parent.Parent
local character = player.Character
local humanoid = character.Humanoid

local function spray()
    local new_part = Instance.new("Part")
    new_part.Name = "Spray"
    new_part.Position = handle.Position
    new_part.Size = Vector3.new(7,2,7)
    new_part.Color = Color3.new(0.0470588, 0.635294, 1)
    new_part.Transparency = 0.8

    new_part.Parent = game.Workspace
end

tool.Activated:Connect(spray)

You also put a parameter in the spray function's signature that you never used, and it wouldn't have worked anyways because the Activated event of a tool doesn't return a player of any sort.

Ad

Answer this question