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

How do I make all the parts in game.Players.LocalPlayer.Character fire with the function "Freeze()"?

Asked by 8 years ago

The code is not right.


player = game.Players.LocalPlayer char = player.Character mouse = player:GetMouse() --Parts head = char:FindFirstChild("Head") torso = char:FindFirstChild("Torso") rleg = char:FindFirstChild("Right Leg") lleg = char:FindFirstChild("Left Leg") rarm = char:FindFirstChild("Right Arm") larm = char:FindFirstChild("Left Arm") --Functions function Freeze(object) if object.Name ~= player.Name or object.Name ~= "Baseplate" then object:Remove() end end mouse.KeyDown:connect(function(key) if key == "f" then if char:GetChildren().ClassName:FindFirstChild("Part") then char:GetChildren().ClassName:FindFirstChild("Part").Touched:connect(function(hit) Freeze(hit) end) end end end)

I'm trying to make whatever part that is touched by the player transparent. I'm going to add more code to the "Freeze" function, but I need to focus on the basics first.

3 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Firstly, KeyDown and Remove() are deprecated. Don't use them in your script. Use UserInputService or ContextActionService in place of KeyDown, and Destroy() in place of Remove(). Here is your code with some small fixes:

player = game.Players.LocalPlayer
char = player.Character
mouse = player:GetMouse()
--Parts
head = char:FindFirstChild("Head")
torso = char:FindFirstChild("Torso")
rleg = char:FindFirstChild("Right Leg")
lleg = char:FindFirstChild("Left Leg")
rarm = char:FindFirstChild("Right Arm")
larm = char:FindFirstChild("Left Arm")
--Functions
function Freeze(object)
    if object.Name ~= player.Name or object.Name ~= "Baseplate" then
        object:Destroy()
    end
end

game:GetService("UserInputService").InputBegan:connect(function(input, gameProcessedEvent) 
    if Input.KeyCode == Enum.KeyCode.F then
        for _, child in pairs(char:GetChildren()) do
            if child:IsA("BasePart") then
                child.Touched:connect(function(hit)
                            Freeze(hit)
                     end)
                    end
        end
    end
end)

If this helped, remember to accept the answer :)

0
Lines 22-24 can be simplified into "child.Touched:connect(Freeze)" (since they both have use the same parameter) chess123mate 5873 — 8y
0
You're right, but I didn't want to touch his code :P DepressionSensei 315 — 8y
Ad
Log in to vote
2
Answered by 8 years ago

In addition to DepressionSensei's answer, if you intend to keep the functionality where pressing 'f' activates the Freeze ability, you should add a flag to make sure you don't :connect things multiple times (which would cause the Freeze function to run multiple times for each object the player comes into contact with). If you want to be able to stop the freeze ability (say by tapping 'f' again), you might do this (expanding on DepressionSensei's input function):

--Lines 1-16 here
local connections
game:GetService("UserInputService").InputBegan:connect(function(input, gameProcessedEvent) 
    if Input.KeyCode == Enum.KeyCode.F then
        if connections then --Disconnect ability
            for i = 1, #connections do
                connections[i]:disconnect()
            end
            connections = nil
        else --Connect ability
            connections = {}
            for _, child in pairs(char:GetChildren()) do
                if child:IsA("BasePart") then
                    table.insert(connections, child.Touched:connect(Freeze))
                end
            end
        end
    end
end)
Log in to vote
0
Answered by 8 years ago

Use a for loop. For example:

for i,v in pairs(game.Players.LocalPlayer.Character:GetChildren()) do
    v.Transparency = 1
end

Now do that in your script.

Answer this question