Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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 :)

2 comments

0

Lines 22-24 can be simplified into "child.Touched:connect(Freeze)" (since they both have use the same parameter)

0

You're right, but I didn't want to touch his code :P

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)

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.