How to make team work with touched script

hello idk what it called but i need help about this.
all i want when player in that team and coal will visible when player touched with truck

but problem
image

script.Parent.Touched:Connect(function(touch)
	local player = game.Players:GetPlayerFromCharacter(touch.Parent)
	if player.Team["Trucker"] then
		if touch.Name == "COAL" then
			touch.Transparency = 0
		end
	end
end)

how to fix it?

3 Likes

there is a error on line 3. Here is how to fix.

Instead of using

if player.Team["Trucker"] then

Use

if player.Team.Name == "Trucker" then

Tell me if this doesn’t work.

1 Like

hmm i think it doesn’t work

A Player is not guaranteed when you use GetPlayerFromCharacter, you nust first check if the Player actually exists.

local player = game.Players:GetPlayerFromCharacter(touch.Parent)
if player then -- if player is not equal to nil
    -- code
end
1 Like

after i using it doesn;t show error anymore but coal part still didnt visible when touched it yet.


and yep im not gud scripting

updated code

script.Parent.Touched:Connect(function(touch)
	local player = game.Players:GetPlayerFromCharacter(touch.Parent)
	if player then
		if player.Team.Name == "Trucker" then
			if touch.Name == "COAL" then
				touch.Transparency = 0
			end
		end
	end
end)

this is where coal located

1 Like

Perhaps it’s just that the coal itself does not touch the right part? try using print(touch.Name) to see which object touches

script.Parent.Touched:Connect(function(touch)
	local player = game.Players:GetPlayerFromCharacter(touch.Parent)
	if player then
		if player.Team.Name == "Trucker" then
			print(touch.Name)
			if touch.Name == "COAL" then
				touch.Transparency = 0
			end
		end
	end
end)
1 Like

i see it only touch Player parts
image

1 Like

maybe you have disabled CanTouch for coal

I’m confused, do you want the coal to be visible or not? And where is the said coal located, if inside character.

1 Like

coal must be visible after touched it but before touch coal will be invisible first
and where coal locate

1 Like

Is the CanTouch coal option enabled?
image

1 Like
script.Parent.Touched:Connect(function(touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(touch.Parent)
	if player then
		if player.Team.Name == "Trucker" then
			local truck = workspace.SpawnedCars:FindFirstChild(Player.Name.."'s Car")
            truck:FindFirstChild("COAL",true).Transparency = 0
		end
	end
end)
1 Like

yes

1 Like

1 Like

I misread please recopy my snippet.

Thank you its working!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.