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

What is wrong with my script?

Im trying to make it to where when you touch this brick you get kicked, But an error shows up saying

Touched/onTouched are not Valid Members of Part. ( Which is the parent.) Here is the Lines of code.

game.Players.PlayerAdded:connect(function(plr)
	script.Parent.onTouched = plr:Kick()
end)

Please Help.

2 answers

✓ Accepted answer

In English, this is what you want to do:

When a player touches a brick, they get kicked

We should formalize this a bit more:

When something touches a brick, if that something is a player's character, then kick that player


Note: there is nothing in that that would respond to a player joining. Think about the problem before you just write something down hoping it will work. It doesn't work like that.

Touched events are events, so they require a connection, just like your use of PlayerAdded.

They are not function values. Beside that, they would need to be a function: the Kick method returns nil so at best this script would be

  • kicks player upon entering
  • set onTouched to nil

When something touches a brick, if that something is a player's character, then kick that player

function onTouch(partTouching)
	if partTouching.Parent then
		local player =
			game.Players:GetPlayerFromCharacter(partTouching.Parent);
		if player then
			player:Kick();
		end
	end
end
script.Parent.Touched:connect( onTouch );

Touched is another event you have to formally call, just like PlayerAdded.

	script.Parent.Touched:connect(function(hit)
		if game.Players:GetPlayerFromCharacter(hit.Parent) == plr then
			plr:Remove()
		end
	end)

However, using PlayerAdded in conjunction with Touched is redundant in this case.

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