Changing team when touched a brick

this is easy, but I can’t seem to figure this out. Also, I’ve never done debouce before so I might’ve messed that up as well.

local debouce = false
script.Parent.Touched:Connect(function(touch)
	debouce = true
	touch.Parent.Team = game:GetService("Teams")["In PVP"]
end)

Team is not a valid member of Workspace “Workspace” is the error I’m getting

3 Likes

touch is the object that touched the part, it is not a player instance

To get the player instead, you would need to do game:GetService("Players"):GetPlayerFromCharacter(touch.Parent), put that in a variable, check if it is not nil, because if the parent of touch is not a character belonging to a player, it nils, if it is not nil, get the Team property from that and then set the team

Something like this

local debouce = false
script.Parent.Touched:Connect(function(touch)
	debouce = true
	local player = game:GetService("Players"):GetPlayerFromCharacter(touch.Parent)
	if not player then
		continue
	end
	player.Team = game:GetService("Teams")["In PVP"]
end)

Also your debounce wont do anything since there’s no condition to check if debounce is true or not. What are you trying to do after teaming? Is this a one-time thing?

yes, it is a one-time thing. after they touch the brick, then the player who has touched it could fight

So If I get you right, if a player touches it, no other player can get the benefits the code gives? If so, just disconnect the event

local event
event = script.Parent.Touched:Connect(function(touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(touch.Parent)
	if not player then
		continue
	end
	player.Team = game:GetService("Teams")["In PVP"]
	event:Disconnect()
end)

If you’re trying to say it only works once for each player, you need a table debounce for that

Oh sorry, I didn’t understand what you meant. multiple players can get it. Also the continue inside of the if statement is an error

Yea I’m a bit late to this since I got busy, I meant to write return and not continue, And for your case, then you just need a table debounce system, something like this

local debounces = {}

script.Parent.Touched:Connect(function(touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(touch.Parent)
	if not player or debounces[player] then
		return
	end
	player.Team = game:GetService("Teams")["In PVP"]
	debounces[player] = true
end)
1 Like

You can use spawn locations (unless you do not want to spawn there). You can set the team change on touch property to be true and select the correct team colour.

Thank you very much. This works just as I wanted :+1:

1 Like