Make multiple People get Affected by Gravity

Hello Scripters!

I had Been working On a Sci-fi Project.

And I Want Multiple Players Get Affected By The Gravity Aka LineForce.

And Yes Im Using One LineForce To Make The Script.

local affectedbygravity = false
game.Players.PlayerAdded:Connect(function(plr)
	local oldplr = plr
	local core = script.Parent
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hrp = char.HumanoidRootPart
	local lf = script.Parent.LineForce
	local mag = (core.Position - char.HumanoidRootPart.Position).Magnitude
	local maxmag = 500 -- Change this to whatever you want
	if plr ~= oldplr then
		local newplr = plr
	end
	while task.wait(0.1) do
		local mag = (core.Position - char.HumanoidRootPart.Position).Magnitude
		print(mag)
		if mag > maxmag then
			affectedbygravity = false
			plr.PlayerGui.PlanetDesc.PlanetName.Text = "Planet Name"
			plr.PlayerGui.PlanetDesc.PlanetDesc.Text = "Planet Description"
			lf.Attachment0 = nil
			lf.Attachment1 = script.Parent.Attachment
			lf.MaxForce = 3500 -- Change this to whatever you want
		else
			affectedbygravity = true
			lf.Attachment0 = hrp.RootAttachment
			lf.Attachment1 = script.Parent.Attachment
			lf.MaxForce = 3500 -- Change this to whatever you want
		end
	end
	if affectedbygravity == true then
		task.spawn(function()
			game.ReplicatedStorage.PlanetRemote:FireClient(plr, "Earth.", "The Self Destroying Planet, Home To Every Human And Animal,")
		end)
	end
end)

Thanks For Visiting Or Helping!

Can you please be clear about your issue? So far you’ve only told us the things about the game, like force, and code but you never tell us the underlying issue

1 Like

So Basically. If Theres Multiple People In 1 Planet. It Will Only Pull 1 Player.

And What I Want To Achieve Is To Make Both Of These Players Get Pulled.

This variable should be within the event (like hrp). Is the code you sent shorted or full version?

I didnt say about the affectedbygravity Variable anything At All.

Nevermind I misread… You should create a new force for every player affected by gravity. Right now it just parents to one player explaining why it only affected one player

Good Idea But. I Dont know how to make it so that all the Players Get Affected By The Force?

I made some edits but this should work:

local affectedbygravity = false
game.Players.PlayerAdded:Connect(function(plr)
	local oldplr = plr
	local core = script.Parent
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hrp = char.HumanoidRootPart
	
	local lf = Instance.new("LineForce")
	lf.MaxForce = 3500
	lf.Attachment0 = plr.CharacterAppearanceLoaded:Wait():FindFirstChild("HumanoidRootPart")
	lf.Attachment1 = script.Parent.Attachment
	lf.Enabled = false
	
	local mag = (core.Position - char.HumanoidRootPart.Position).Magnitude
	local maxmag = 500 -- Change this to whatever you want
	if plr ~= oldplr then
		local newplr = plr
	end
	while task.wait(0.1) do
		local mag = (core.Position - char.HumanoidRootPart.Position).Magnitude
		print(mag)
		if mag > maxmag then
			affectedbygravity = false
			plr.PlayerGui.PlanetDesc.PlanetName.Text = "Planet Name"
			plr.PlayerGui.PlanetDesc.PlanetDesc.Text = "Planet Description"
			
			lf.Enabled = false
		else
			affectedbygravity = true
			lf.Enabled = true
		end
	end
	if affectedbygravity == true then
		task.spawn(function()
			game.ReplicatedStorage.PlanetRemote:FireClient(plr, "Earth.", "The Self Destroying Planet, Home To Every Human And Animal,")
		end)
	end
end)

yeah that script kinda didnt work.

Is the script printing the magnitude and are there errors?

first of all. no its not printing. and secend of all yes there is

this part right here is causing bugs since you probbably didnt change it to this

lf.Attachment0 = plr.CharacterApperanceLoaded:Wait():FindFirstChild("HumanoidRootPart").RootAttachment

since it needs an attachment

Yea I just realized I forgot to index the attachment lol

Can you send the full script so I can re-arrange some stuff the code looks messy and hard to read. (If you want to and I’m not stealing your code lol)

that was the entire script i showed.

i think you meant the local script

local ts = game:GetService("TweenService")
game.ReplicatedStorage.PlanetRemote.OnClientEvent:Connect(function(plr, planetname, planetdesc)
	plr.PlayerGui.PlanetDesc:GetDescendants().Visible = true
	plr.PlayerGui.PlanetDesc.PlanetName.Text = planetname
	plr.PlayerGui.PlanetDesc.PlanetDesc.Text = planetdesc
	ts:Create(plr.PlayerGui.PlanetDesc:GetDescendants(), TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.In, 0, false, 0), {Transparency = 1})
	ts.Completed:Connect(function()
		plr.PlayerGui.PlanetDesc:GetDescendants().Visible = false
	end)
end)

Try this:

local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local PlanetRemote = RS.PlanetRemote 

local Core = script.Parent
local MaxMag = 500

Players.PlayerAdded:Connect(function(player: Player) 
	local Character = player.Character or player.CharacterAppearanceLoaded:Wait()
	local Hrp = Character.HumanoidRootPart
	
	local affectedbygravity = false
	local Previous = nil
	
	local Lf = Instance.new("LineForce")
	Lf.MaxForce = 3500
	Lf.Attachment0 = Hrp.RootAttachment
	Lf.Attachment1 = Core.Attachment
	
	while task.wait(0.1) do
		local Mag = (Core.Position - Hrp.Position).Magnitude 
		print(Mag)
		
		affectedbygravity = (Mag <= MaxMag) and true or false
		
		if affectedbygravity == Previous then continue end
		
		if affectedbygravity then
			Lf.Enabled = true
	
			PlanetRemote:FireClient(player, "Earth.", "The Self Destroying Planet, Home To Every Human And Animal.")
		else	
			Lf.Enabled = false
			
			PlanetRemote:FireClient(player, "Planet Name.", "Planet Description")
		end	
		
		Previous = affectedbygravity
	end	
end)