Change character when pressing F

Why did you put it in a while loop? And I’m failing to see why you need a different character for the player to fly.

Because the code needs to know if its still flying if I make it like if enabled == true then it will not keep flying. So thats why its while enabled == true do

And I need to change the character because I have a character that has a Light Bubble and particles so when de player starts flying it becomes a speedy light bubble. Don’t know how I could do it without this method?

The reason it’s creating a lot of characters is that you’re creating ~60 characters per second since you put the character creation code in a while loop.

You could achieve the same effect without creating a new character by simply adding the particle effect in the character’s humanoidrootpart when flying, and removing it afterwards. No need for a new character.

1 Like

Yes thats true.

But then I only want a light bubble to appear when flying. I cant even imagine how to do this!
Because if just put it in humanoidrootpart everything else is still visible right, like legs, head, whole body? And how can I put it inside the humanoidrootpart and delete it or turning it off?

I think you can set everything else to be invisible using the Transparency property then set it back to visible after the player has finished flying.

for _, inst in ipairs(character:GetDescendants()) do
    if inst:IsA("BasePart") or inst:IsA("Decal") then
        inst.Transparency = 1
   end
end

Then do the opposite after the player finishes flying.

1 Like

Thank you that works almost perfect!

Only problem what happens when you press f it flies and it will be invisible!
But then when you stop flying. the player looks fine too but there is a part or something that only comes when you set transparency back to 0. Like if this is something that comes from roblox itsself?

And do you know how I can ajust the part to the humanoidrootPart?

repeat wait() until game.Players.LocalPlayer.Character

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local torso = character:WaitForChild("LowerTorso")
local mouse = player:GetMouse()
local enabled = false

mouse.KeyDown:Connect(function(key)
	if key == "f" then
		if enabled == false then
			enabled = true
			local bodyvelocity = Instance.new("BodyVelocity", torso)
			bodyvelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			while enabled == true do
				for _, inst in ipairs(character:GetDescendants()) do
					if inst:IsA("BasePart") or inst:IsA("Decal") then
						inst.Transparency = 1
					end
				end
				bodyvelocity.Velocity = mouse.Hit.LookVector * 100
				wait()
			end
		end

		if enabled == true then
			enabled = false
			
			torso:FindFirstChild("BodyVelocity"):Destroy()
			
			for _, inst in ipairs(character:GetDescendants()) do
				if inst:IsA("BasePart") or inst:IsA("Decal") then
					inst.Transparency = 0
				end
			end
		end
	end
end)

for the second loop removing the transparency change the if statement to this:
if inst:IsA("BasePart") and not inst.Name == "HumanoidRootPart" or inst:IsA("Decal") then

This will prevent the humanoidrootpart from getting set to visible.

1 Like

What am I doing wrong?


repeat wait() until game.Players.LocalPlayer.Character

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local torso = character:WaitForChild("LowerTorso")
local mouse = player:GetMouse()
local enabled = false

mouse.KeyDown:Connect(function(key)
	if key == "f" then
		if enabled == false then
			enabled = true
			local bodyvelocity = Instance.new("BodyVelocity", torso)
			bodyvelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			while enabled == true do
				for _, inst in ipairs(character:GetDescendants()) do
					if inst:IsA("BasePart") or inst:IsA("Decal") then
						inst.Transparency = 1
					end
				end
				bodyvelocity.Velocity = mouse.Hit.LookVector * 100
				wait()
			end
		end

		if enabled == true then
			enabled = false
			
			torso:FindFirstChild("BodyVelocity"):Destroy()
			
			for _, inst in ipairs(character:GetDescendants()) do
				if inst:IsA("BasePart") and not inst.Name == "HumanoidRootPart" or inst:IsA("Decal") then
					inst.Transparency = 0
				end
			end
		end
	end
end)

I fixed it for you. There was an issue with the second if statement. I also changed the enabled if statement an if else, so the code doesn’t run twice.
I also added some checks to make sure that the script doesn’t break when the player dies.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")
local torso = character:WaitForChild("LowerTorso")
local mouse = player:GetMouse()
local bv = nil
local enabled = false

player.CharacterAdded:Connect(function(c)
	character = c
	humanoid = c:WaitForChild("Humanoid")
end)

mouse.KeyDown:Connect(function(key)
	if key == "f" then
		if enabled == false then
			enabled = true
			local bodyvelocity = Instance.new("BodyVelocity", torso)
			bodyvelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			while enabled == true do
				for _, inst in ipairs(character:GetDescendants()) do
					if inst:IsA("BasePart") or inst:IsA("Decal") then
						inst.Transparency = 1
					end
				end
				bodyvelocity.Velocity = mouse.Hit.LookVector * 100
				wait()
			end
		else
			enabled = false

			torso:FindFirstChild("BodyVelocity"):Destroy()
			
			for _, inst in ipairs(character:GetDescendants()) do
				if inst:IsA("BasePart") and inst.Name ~= "HumanoidRootPart" or inst:IsA("Decal") then
					inst.Transparency = 0
				end
			end
		end
	end
end)

Ideally, it might be a good idea to replace mouse.KeyDown with UserInputService and replace the while look with a Heartbeat event, but you can look into that yourself when you want to.

1 Like

Thank you sooo much that works great!

So now only one thing thats needs to be done. And thats when the player is flying welding a part to the humanoidrootpart.
Or when the player character is added just turn it on and off.

What I found was:

local shield = game.ReplicatedStorage.Particle:Clone()

game.Players.PlayerAdded:Connect(function(player)

local character = player.Character or player.CharacterAdded:Wait()

shield.Parent = character

local weld = Instance.new("WeldConstraint")

weld.Parent = character

weld.Part0 = shield

weld.Part1 = character.HumanoidRootPart

shield.Position = character.HumanoidRootPart.Position

shield.IsInUse.Value = true

end)

But dont know where to put this haha and maybe it wont even work like this what do you think?

Can you tell me how the particle effects are structured? Is it only an attachment with particles inside?

1 Like

image

So Part, Attachment, ParticleEmitter
attachment cant be placed without being parented to a partInstance

You could parent the attachment to the HumanoidRootPart

local attach = PathToAttachment:Clone()
attach.Parent = character.HumanoidRootPart

Then simply destroy it afterwards.

1 Like

Only problem with this is that im using a while loop so it will spawn every sec like you said before. And the part is not following the player

Parent the attachment, not the part.
And you only need to parent it once before the while loop.

1 Like

Once again! Thank you so much this works great now. And for the deleting part I did this:

else
			enabled = false
			local findPart = character:FindFirstChild("HumanoidRootPart").Attachment
			findPart:Destroy()

			torso:FindFirstChild("BodyVelocity"):Destroy()

			for _, inst in ipairs(character:GetDescendants()) do
				if inst:IsA("BasePart") and inst.Name ~= "HumanoidRootPart" or inst:IsA("Decal") then
					inst.Transparency = 0
				end

is

local findPart = character:FindFirstChild("HumanoidRootPart").Attachment

okay to use?

If everything works right, it should be.

1 Like

Thank you! Only problem now is when the player dies. It wont work anymore. Even the fly part doesnt work?

Can you show me what the code looks like now?

1 Like

Okay so the problem was the localscript was inside starterPack!
Now I changed it to starterCharacterScripts (And now when you die it reset nicely)

Only problem now is, only the player that flies sees the light bubble. Look at the image below

How to fix this?

My code (inside StarterCharacterScripts → LocalScript:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")
local torso = character:WaitForChild("LowerTorso")
local mouse = player:GetMouse()
local bv = nil
local enabled = false

player.CharacterAdded:Connect(function(c)
	character = c
	humanoid = c:WaitForChild("Humanoid")
end)

mouse.KeyDown:Connect(function(key)
	if key == "f" then
		if enabled == false then
			enabled = true
			local bodyvelocity = Instance.new("BodyVelocity", torso)
			bodyvelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			local attach = game.ReplicatedStorage.Particle.Attachment:Clone()
			attach.Parent = character.HumanoidRootPart
			
			
			while enabled == true do
				for _, inst in ipairs(character:GetDescendants()) do
					if inst:IsA("BasePart") or inst:IsA("Decal") then
						inst.Transparency = 1
						
					end
				end
				bodyvelocity.Velocity = mouse.Hit.LookVector * 150
				wait()
			end
		else
			enabled = false
			local findPart = character:FindFirstChild("HumanoidRootPart").Attachment
			findPart:Destroy()
			torso:FindFirstChild("BodyVelocity"):Destroy()

			for _, inst in ipairs(character:GetDescendants()) do
				if inst:IsA("BasePart") and inst.Name ~= "HumanoidRootPart" or inst:IsA("Decal") then
					inst.Transparency = 0
				end
			end
		end
	end
end)