Touched event not working

why is this happening?

local shield = script.Parent
local player = game.Players.PlayerAdded:Wait()

shield.Touched:Connect(function(hit)
	if hit.Parent == hit.Parent:FindFirstChild("Bullet") then
		player.BulletHit.Value = player.BulletHit.Value - 1
		repeat wait() until player.BulletHit.Value == 0
	end
	if player.BulletHit.Value == 0 then
		shield:Destroy()
	end
end)

Im getting no errors but it’s not doing anything, why?

2 Likes

A few things I’m not understanding,

What’s with the game.Players.PlayerAdded:Wait()? What is that trying to do? It’s jsut going to wait for a player to be added before continuing

if hit.Parent == hit.Parent:FindFirstChild("Bullet") then will probably not be met since they’d not be equal since they’re different instances in this case

Final question, what are you even trying to do in the first place? I’m not understanding the code that well

1 Like

so im trying to make a shield that protects the player, but im not sure how to make it so the player doesn’t take any damage while the shield is on. I figured out the problem to this just now, i put this script into serverscriptservice and changed a few things.

1 Like

Any suggestions on how to prevent the player from taking any damage while the shield is on?

Set the player health to something like 500 such as:

while ShieldIsOn == true do
  Player.Character.Humanoid.Health = 500
end

How is the shield set up exactly? Is it a part of the player’s character? It could be an external part that the player is inside off, and then jsut add a check in your weapons that check if what is it is a shield, and if it was, just hurt the shield.

If you have multiple checks i nyour weapons, just make the shield check have priority

it’s a part on the player’s character and has a value in the part

is there another way to do it? other than that

Not that I know of but I think the while (condition == true) do loop will work for you’re needs.

Then I recommend in your weapon checks, check if the name of the part hit was called “Shield”, and if it was, make it decrease the value in the shield part, And in a script i nthe part, make it listen for changes to the Value’s value, and if the value ever goes 0 or below, destroy the shield

but how do i prevent the humanoid from taking any damage while the shield is on?

I think again, you can make whatever is trying to deal damage check if the name of the part that’s being hit is the same name as the name of the shield part if you only have one part as the shield, otherwise, check the name of the parent of the part is equal to the name of the shield. If you give the shield check priority before others, it’ll prevent the humanoid from eing
damaged

Example

if hit.Name == "Shield" then
   --decrease value in shield part
elseif hit.Parent:FindFirstChildOfClass("Humanoid") then
   --Damage humanoid
end

would it be like this?

local player = game.Players.PlayerAdded:Wait()
local debounce = false
local player = game.Players.PlayerAdded:Wait()
local char = player.Character or player.Character:Wait()
script.Parent.Touched:Connect(function(hit)
	if hit.Name == "Shield" then
		player.BulletHit.Value -= 1
	elseif hit.Parent:FindFirstChildOfClass("Humanoid") then
		char.Humanoid = char.Humanoid - 10
	end
end)

No…What is this script even for the shield? That’s not how it should be done. The weapons you have currently/enemies, those should be the ones that check if what they hit is a shield, not the shield itself, because they can still damage you without the check

You could make it so the humanoid has infinite health, while having the shield equipped. If it is a gear, you could use the Equipped Function.

this script is for is for the bullet

Oh, then another question, why is the bullet checking for players? It doesn’t have to do that, it should only check what it has hit. Also, you should debounce this since it’ll do continous damage to the shield and humanoid

You could create a bool value within the player’s character itself.

When the shield is active set it to true and vice versa.

Now in the code you could do something a long the lines of

if hit.Parent:FindFirstChild("ShieldActive").Value then
    --Dont Damage
end

isn’t that what the script was doing?

You don’t need the PlayerAdded and Character lines, those aren’t needed if this is a bullet