How to detect if a player has immunity on respawn?

I’m having trouble getting a Projectile to Detect if a player has that few seconds of immunity when they respawn. I have tried to check if the opposing player has a force field in their character, but nothing is happening.

if hit:IsA("ForceField") then
	print("ug")
else
	explosion.BlastPressure = 1
	kills.Value = kills.Value + 1
	rocket:Destroy()
end
2 Likes

You can get the player from the character, then check if there’s a force field inside of the player.
like this:

script.Parent.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if(player) then
		if(player.Character:FindFirstChild("ForceField")) then

		else
			--Your code here
		end
	end
end)
1 Like

thanks, but i have already gotten the character from the rocket.

Then just check if the character contains the force field.
Something like this:

if(Character:FindFirstChild("ForceField")) then
1 Like

Thanks, but it was because I `misspelled something elsewhere

1 Like

Use

if not player.Character:FindFirstChild("ForceField") then

end

That’s basically what I said, but he wanted to print if there’s a force field. so I used it without not and with else.

Ok. I noticed you have a few random brackets dotted through your script?

Should there be a bracket after if?

Yes, thanks for telling me about it :slightly_smiling_face:, I missed a bracket in the end. I am kinda used to write ifs with brackets. so I did not notice. It can be written without brackets, or with, it does really not matter

1 Like

Some programming languages enforce the use of brackets after IF statements for their respective conditions. I think that trend has traversed into ROBLOX Lua.

Either method of validating conditions works!
:slightly_smiling_face:

3 Likes

That’s correct Nidoxs :slightly_smiling_face:. I also program in Java, so I usually use the brackets in Roblox as well, it’s a habit of mine :slightly_smiling_face:.

1 Like

Yeah, a lot of like minded programmers that use additional languages other than Lua seem to have this habit. Again I think it’s perfectly fine which method anyone uses, it’s all down to personal preference really. :love_you_gesture:

2 Likes

Use this instead, as the others rely on the naming of the ForceField instance.

if not Character:FindFirstChildOfClass"ForceField" then

end

Why? There’s only one force field… and it’s named force field, there aren’t any other parts named force field in there. Both ways do the same thing, so why instead?

It’s always better to find these instances by their ClassNames than their Names unless you absolutely need to find them via their names. You never know if the name is going to change (usually happens when you’re working with another developer) and it’s better for universality.

1 Like

That’s true that names can change, but it’s also easy to solve, just change the name, and also, classes can also change, and new classes are being added to replace them as well sometimes I believe, just like services, a new one added, an old one get deprecated.

The difference is that one will most likely never change and that the other can be changed at any time. This is why finding things via their ClassNames is much, much more reliable.

1 Like

What’s their interest to change names often and at any time? And also, aren’t services classes too? They get changed many times as well. For example, GamePassService and MarketplaceService. And I am sure many classes refer to the force field by it’s name, so why will they change it?

Depending on your use case, FindFirstChildWhichIsA, which will also account for inherited classes. Anything to do with class strictly checks the ClassName, IsA will run IsA and account for any instance’s superclasses. For example, ScreenGui IsA LayerCollector.

I think this conversation’s pretty off topic at this point anyway, just wanted to throw a quick tidbit. Mind moving to DMs? cc @LightningLion58

2 Likes