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

Colour and Particles aren't affected?

Asked by 5 years ago
Edited 5 years ago

So I've recently started to learn Lua via a combination of friends teaching me, devforums articles and YouTube tutorials and one of the tasks I set myself in order to learn was to make a tool which kills a player then changes their part colours to blue and causes them to emit particles. I have attempted to script one however it only kills the player instead of changing their colour and emitting particles. Here is my code:

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
script.Parent.Handle.Touched:Connect(function(hit)
    if hit.Parent.Humanoid then
        hit.Parent:BreakJoints()
        local colorful=hit.Parent:GetChildren()
        if colorful.ClassName=="Part" then
            colorful.BrickColor=BrickColor.new("Electric blue")
            local particle = Instance.new("ParticleEmitter")
            particle.Parent=colorful        
        end
    end
end)

1 answer

Log in to vote
0
Answered by
Lazarix9 245 Moderation Voter
5 years ago

Now, this might be a bit too advanced for a beginner, but here's what I could do:

local tool = script.Parent
local handle = tool:WaitForChild("Handle")

script.Parent.Handle.Touched:Connect(function(hit)

    local players = game:GetService("Players")

    if hit.Parent:FindFirstChild("Humanoid") then

        local humanoid = hit.Parent.Humanoid
        local plr = players:GetPlayerFromCharacter(hit.Parent)

        print("humanoid")

        hit.Parent:BreakJoints()

        plr.CharacterAdded:Wait()

        print("added")

        local bodyColors = plr.Character["Body Colors"]
        local color = BrickColor.new("Electric blue")
        local changeColor = {

            HeadColor = color,
            LeftArmColor = color,
            RightArmColor = color,
            LeftLegColor = color,
            RightLegColor = color,
            TorsoColor = color

            }

        local colorful=plr.Character:GetChildren()

        for i = 1, #colorful, 1 do

            if (colorful[i]:IsA("Part") or colorful[i]:IsA("MeshPart")) and (not colorful[i]:FindFirstChild("ParticleEmitter")) then

                print(colorful[i].Name)

                local particle = Instance.new("ParticleEmitter")
                particle.Parent= colorful[i]

            end
         end

        print("yay")

        for k, v in pairs(changeColor) do

            bodyColors[k] = v

        end



    end
end)

This is very confusing I recommend not reading below

Let me explain it in detail. You already know how to set up a listener, so we'll skip that part and how to check for humanoid, we'll skip those parts.

CharacterAdded is also a listener. It listens for when a players character loads. This time, you may have noticed, we don't use :Connect() to connect it to a function, we use :Wait() to make it wait for when the event happens.

Now, I'm not sure if you know what this is, so I'm gonna explain it anyways. In order to explain what a dictionary is, you're gonna need to know what an array is first. An array (or table) is like a set of values which you can index from numbers 1 to the length of the array (I did my best ;-;). A dictionary is kinda like that but instead of numbers, you have a key and a value (kinda like variables). Just so you know, to get the length of a table or a dictionary we use # thingy.

Next, we get the children of the player's character using GetChildren(). GetChildren() will return an array of the children.

Then a for loop from 1 to the length of the colorful array. It will check if the part is a part or meshpart and if a particleemitter doesnt exist already in it, and if all that's true it will put a particleemitter in it.

After that is another for loop which will get the key and value from the dictionary changeColor and it will change the color.

Yeah, I'm bad at explaining.

Why your script didn't work

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
script.Parent.Handle.Touched:Connect(function(hit)
    if hit.Parent.Humanoid then
        hit.Parent:BreakJoints()
        local colorful=hit.Parent:GetChildren()
        if colorful.ClassName=="Part" then
            colorful.BrickColor=BrickColor.new("Electric blue")
            local particle = Instance.new("ParticleEmitter")
            particle.Parent=colorful        
        end
    end
end)

at line 6, colorful is defined. As mentioned earlier, GetChildren() returns an array. at line 7, arrays don't have ClassNames. line 8, arrays don't have BrickColors either. line 10, I don't think you can parent anything to them.

Ad

Answer this question