Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

W001: Unkown global 'PlayerTouched', fix?

local Brick = script.Parent
game.Players.PlayerAdded:Connect(function(player)
if not player:IsInGroup(157764) then
local function PlayerTouched(Part)
	local Parent = Part.Parent
	if game.Players:GetPlayerFromCharacter(Parent) then
			Parent.Humanoid.Health = 0
	end
end
end
end)

Brick.Touched:connect(PlayerTouched)

5 comments

0

If one of the answers helped you out please accept their answer. It is considered a courtesy and shows the community you no longer need help.

0

Do you have a discord account?

0

Yes, I do, vonotige is dieng#5938

0

mine is PyroPhlegethon#6268

0

I could not find yours.

2 answers

✓ Accepted answer

Your issue is one of scope. The function is defined locally inside of a block of code. This means that your code will only see that function inside of the block that it was defined in. Here is my best explanation of scope. I don't know what you're trying to do, but because you have the :GetPlayerFromCharacter() you don't need it inside the PlayerAdded event. In fact, you are just creating additional unnecessary events every time a new player joins. I recommend putting the function outside of the PlayerAdded event. I hope this helps and have a great day scripting! Note: read the link I posted to more clearly understand how scope works.

2 comments

0

As for the if statement you created, just put it into the touched function after creating a variable for player.

0

I'm trying to make a group only door, if you're not in the group it kills you, it would be nice if you could help me out.

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		if not hit.Parent:IsInGroup(157764) then
			hit.Parent.Humanoid:TakeDamage(100)
		end
	end
end

script.Parent.Touched:connect(onTouched)

1 comment

0
  1. Nice explanation. 2. findFirstChild and connect are deprecated. Use FindFirstChild and Connect.

This discussion is preserved as it was. New answers, comments, and votes are closed.