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

My left arm won't turn things into gold?

Asked by
Probix 80
8 years ago

I try to make a script where my left arm would turn parts into the gold color, but it seems I might be doing something wrong... it doesn't do anything!

MagicArm = game.Workspace.Player1["Left Arm"]

function Golden ()
    game.Workspace.Part.BrickColor = BrickColor.new("Gold")
end

MagicArm.Touched:connect(Golden)

This might be easy, but bare with me, I can be pretty stupid....

1 answer

Log in to vote
6
Answered by 8 years ago

Machine type

Anything involving strict user-to-player or player-to-world type functionality should be handled with a local script. The only thing server scripts should be used for, should be handling, processing, or passing information from or between players.


This isn't very important right now, and I'm not going to make this answer all about networking, but it is something you should keep very close attention to. Deciding what side of the network (i.e, client or server) your code runs on is very important (especially when FilteringEnabled is brought into the picture). For my answer, it will be assuming this code is inside of a local script.

Utilizing local script features

Another benefit to using local scripts (besides their speed and efficiency when handling user input), is the built-in LocalPlayer property of the Players service. This property will return the player the local script is a descendant of, where you can also access it's character. Here's an example:

-- Gets the local player
local Player = game:GetService("Players").LocalPlayer

-- Waits for the character, and saves it to the "Character" variable
local Character = Player.Character or Player.CharacterAdded:wait()

(In case you wanna know more about my method for getting the character from the player shown above, check here: https://scriptinghelpers.org/questions/28688/why-is-char-nil#32125)

Now we can access whatever components we want from the character, in this case, the "Left Arm". We should do this by using the WaitForChild method, just in case our script tries to get the arm before it actually loads:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()

-- Get the right arm when it loads
local LeftArm = Character:WaitForChild("Left Arm")

Final result

Other than that, you kind of had the right idea, though there are some features we should take advantage of here. First, the argument the Touched event returns to the function. We can access this argument by giving our callback function a parameter, that could be anything. For readability sake, we'll call it "Hit"

LeftArm.Touched:connect(function(Hit) -- We'll also use an anonymous function instead, since we don't need a reference to this anymore.

end)

We'll also want to make sure the Left Arm isn't hitting the character's body parts, so we'll add an if statement condition to check where the Hit's parent is:

LeftArm.Touched:connect(function(Hit)
    if Hit.Parent ~= Character then -- If whatever his the Left Arm isn't parented to the character, then...
    end
end)

Now let's apply this to our final product:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local LeftArm = Character:WaitForChild("Left Arm")

LeftArm.Touched:connect(function(Hit)
    if Hit.Parent ~= Character then
        Hit.BrickColor = BrickColor.new("Gold") -- Change the color
    end
end)

Hope this helped, let me know if you have any questions.

0
Thank you very much! Your answer was very informational, and I got it now. Thanks. Probix 80 — 8y
0
Np, I also noticed I used "Right Arm" instead of "Left Arm" a few times, I fixed that. ScriptGuider 5640 — 8y
0
Lol, right, left, whatever. User#11440 120 — 8y
Ad

Answer this question