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

How do I make a player from a certain team touch to change decal color?

In full detail, as Roblox has implemented a ImageColor3 feature for Decals/Textures/ImageLabels, I'm trying to make the ImageColor3 change when its touched by a player from a certain team.

So for example, if a player is from blue team and has touched the block containing the decal, the decal will turn blue as well.

This is the script I've typed so far but I'm not so sure where has gone wrong, any help perhaps?

function OnTouched()
	if Player.Team == game.Teams[Cytrons]
		then
		script.Parent.BillboardGui.ImageLabel.ImageColor3 = Color3.new(0,161,255)
	else
		script.Parent.BillboardGui.ImageLabel.ImageColor3 = Color3.new(174,0,255)
	end
end

script.Parent.Touched:connect(OnTouched)

1 answer

✓ Accepted answer

There are just multiple small problems within your code but the logic is ok, the main problem is that we do not find the player from the touched part ie when a player touches the brick we get the touched part as an argument that we use to find the player.

Getting a player object can be done by using GetPlayerFromCharacter. This will pass back the player or nil.

Example:-

function OnTouched(hit) -- hit will be the part that touched the part
	local char = game.Players:GetPlayerFromCharacter(hit.Parent)  -- hit is the part, its parent should be the players model which is what we need
	print(char)
end

script.Parent.Touched:connect(OnTouched) -- the event passes one argument 

As we now can get the player we next need to choose which color to set the image label, we could check the name of the team or the TeamColor which it a BrickColor. Eather method is ok.

Example:-

if player.Team.Name == 'TeamA' then
	-- run code
end

if player.Team.TeamColor == BrickColor.new([constructor]) then
	-- code
end

Lastly Color3 has a range of 01 but rgb has a range of 0255 if you are using the constructor Color3.new() we need to / 255 to get the range of 0~1 or you can use an alternative constructor Color3.fromRGB() a list of constructors can be found here.

Putting this all together:-

function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- try to get the player
	if plr then	-- we have a player ie not nil
		if plr.Team.Name == 'Cytrons' then -- compare the team name
			script.Parent.BillboardGui.ImageLabel.ImageColor3 = Color3.fromRGB(0,161,255)
		else
			script.Parent.BillboardGui.ImageLabel.ImageColor3 = Color3.fromRGB(174,0,255)	
		end
	end
end

script.Parent.Touched:connect(OnTouched)

This is a very basic example and this can be improved upon such as using variables and getting the services.

I hope this helps.

1 comment

0

Does this affect R15 users as while I test it in Studio since the default rig is R15?

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