Forcefield not being given to player

I’m in the middle of coding an invincibility system, and instead of doing something complicated I rather use forcefields. The problem is the script doesn’t seem to give the forcefield


for _, v in ipairs(players) do
			local ff ="ForceField")
			ff.Visible = true
			ff.Name = "ForceField"
			ff.Parent = v.Character
			debris:AddItem(ff, 1000000000)

Is the parent of the forcefield actually the character? Have you tried printing variables out and things to see where the forcefield is in your game

Here are a few things you can check to troubleshoot the problem:

  1. Ensure that the players variable contains the desired players you want to apply the force fields to. You can print the players variable to verify if it contains the correct player objects.

  2. Confirm that the Character property of each player is not nil. If a player’s character hasn’t loaded yet, the Character property will be nil, and you won’t be able to parent the force field to it. Make sure you’re applying the force field after the character has been fully loaded.

i think players variable is the service not the players:GetPlayers()

After adding print statements, it seems that the loop never runs

Full script:

local event = game.ReplicatedStorage.RemoteEvents.modChange
local mods = game.ServerStorage.Mods
local reset = game.ReplicatedStorage.bindableEvents.resetMods
local invin = game.ReplicatedStorage.invincibility
local players = game:GetService("Players"):GetPlayers()
local debris = game:GetService("Debris")

event.OnServerEvent:Connect(function(player, command)
	if command == "speedUp" then
		mods.Time.Value = mods.Time.Value * 0.5
		print("Speed Multiplied!")
	elseif command == "invincibility" then
		mods.Invincibilty.Value = true
		invin.Value = true
		print("invincibility enabled!") -- Last thing that runs
		for _, v in ipairs(players) do
			local ff ="ForceField")
			ff.Visible = true
			ff.Name = "ForceField"
			ff.Parent = v.Character
	elseif command == "gravity" then
		game.Workspace.Gravity = 30

	mods.Time.Value = 1
	mods.ExtraTime.Value = 0 
	mods.Invincibilty.Value = false
	invin.Value = false
	mods.Gravity.Value = 140

I ran a print and it looks like the players variable is empty which is really weird.

local players = game:GetService("Players"):GetPlayers()

this is my code

Fixed it. It was because :GetPlayers gives an array of the players when the server starts, and not when it’s called. Just wait to use :GetPlayers until you actually need it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.