Touched event not working

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

so what do i do instead of that?

You just need to have the touched code and the debounce code if this is a bullet, nothing else. The bullet should only detect if something was a shield or not.

If you need to get the player, just use GetPlayerFromCharacter, a method of the players service, on the Parent of hit and do stuff with that

what about for this part

	if hit.Name == "Shield" then
		player.BulletHit.Value -= 1

it’s a value in the player

You use GetPlayerFromCharacter as I mentioned to get the player via hit.Parent, it may return nil if there was no player with that character, so use an if statement to check if what was hit was a character and if not, do nothing.

The article should hae an example of the use of it

Another way you could achieve this is by creating a bool value that detects if the shield is equipped, and if it is, then the player would take no damage(modify the damage script in the gun).

1 Like

I had this response literally above, feels bad he didn’t see it

1 Like

LMAO feels bad… Great minds think alike, though.

That function does not return a player and just waits for a player to join, also it only works once.
You’ll need to connect a PlayerAdded function to get your player.

how do i prevent it from taking damage is my question

On your hit code do

if hit.Parent:FindFirstChild("ShieldActive").Value then
    return;
else
    DoDamage
end

Yeah, make humanoid.MaxHealth and humanoid.Health equal to math.huge().

:Wait on RbxScriptSignals waits for something, and then returns it. So in this case, once a player joins, it returns that player.

1 Like