Making my first game (Soccer game)

My issue is that I have a clickdetector in a frame and I want it so when a person clicks the button, it changes their player.team property to a team.

I also have two scripts.

One of the scripts is a serverscript and instantly makes the player a spectator.

local players = game:GetService("Players")
local spec = game.Teams.Spectator
players.PlayerAdded:Connect(function(player)
	if player ~= spec then
		player.Team = spec
	else
		print(player.Name)
	end

end)

The second code is a localscript and the localscript is in the same hierarchy as the clickdetector.

local clickdetector = game.StarterGui.ScreenGui.Red.ClickDetector
local team = game:GetService("Teams")
local red = team.Red
local char = game.Players.LocalPlayer.Character
local humanoids = char:FindFirstChild("Humanoid")

--game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
	--if character:IsDescendantOf(game.Workspace) then
		--print("Char is added
		clickdetector.MouseClick:Connect(function(player)
			print(player.AccountAge)
			player.Team = red
			print("This line must print if works properly")
		end)
	--end
--end)

The printing is there for debugging.

I do not get any errors in my output. The clickdetector doesn’t seem to work. The curser changes when I hover over the frame but when I click on it with LMB, it doesn’t change my team to red.

Any solutions? Thank you in advance.

Edit : I believe the error is in localscript or the error is that the localscript code is getting overriden by the serverscript code and hence not running.

If that is the problem, I could just do this write:

local players = game:GetService("Players")
local spec = game.Teams.Spectator
local myevent

myevent = players.PlayerAdded:Connect(function(player)
	if player ~= spec then
		player.Team = spec
	else
		print(player.Name)
        myevent:Disconnect()
	end

end)

Why are you using a ClickDetector in a frame? Just use a TextButton. Then you can use TextButton.Activated to get if the button was activated.

Oh? I didn’t know that sorry. I am a new coder. I will try that.

Do I put the code inside the textbutton or in the same hierarchy as it?

Code still doesn’t work or there might be an issue in the way I coded it

local textbutton = game.StarterGui.ScreenGui.Red.TextButton
textbutton.Activated:Connect(function()
		game.Players.LocalPlayer.Team = game.Teams.Red
end)
1 Like

Problem with code found here.

local players = game:GetService("Players")
local spec = game.Teams.Spectator

players.PlayerAdded:Connect(function(player)
	if player.Team ~= spec then -- I changed this part, you forgot "player.Team"
		player.Team = spec
	else
		print(player.Name)
	end
end)
``

Thanks, I will see.

chars chars chcars

Sorry for the late response, that script now successfully works. But, whenever I click on the textbutton, it still doesn’t change my team to red.

I changed the code of the localscript that should change my team to red. But doesn’t work?

local textbutton = game.StarterGui.ScreenGui.TextButton
textbutton.Activated:Connect(function()
		game.Players.LocalPlayer.Team = game.Teams.Red
       print("Firing")
end)

Where are you changing the player’s team from?
If client, then make a remote request to the server changing the teams.

Also why use .Activated? Use .MouseButton1Down

I don’t think clickdetectors allow mousebutton1down anymore or smth like that. There is mouseclick tho.

Yeah, I recommend using MouseClick, didn’t know it was a Click Detector.

Also, your script is weird…

It’s mentioning a TextButton, not a click detector / part?

It is because I removed the clickdetector and instead put a textbutton because somebody recommended it.

As you insist on using .MouseClick then shouldn’t this code work?

local clickdetector = game.StarterGui.ScreenGui.Red.ClickDetector
local team = game:GetService("Teams")
local red = team.Red
local char = game.Players.LocalPlayer.Character
local humanoids = char:FindFirstChild("Humanoid")

--game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
	--if character:IsDescendantOf(game.Workspace) then
		--print("Char is added
		clickdetector.MouseClick:Connect(function(player)
			print(player.AccountAge)
			player.Team = red
			print("This line must print if works properly")
		end)
	--end
--end)

it doesn’t tho.

I’m pretty confused on what your doing, can you send a screen shot of the workspace paths, and explain what your doing somewhat.

I see the problem, you put a click detector in the GUI, that’s not how it works…

Oh? Where do I do it then?

chars chars

I recommend you read this article about ClickDetectors, not required though.

If it’s a gui, then it should be a TextButton, whoever told you to make it a click detector is prob just trolling / inexperienced.

What do you mean by if it is a gui, it should be a textbutton. Do you mean inside the screengui should be a textbutton and inside the textbutton should be a clickdetector.

Again, can I see what your doing? Show me the layout of your explorer.

And no, that’s not what I mean at all. It should be a TextButton inside of a ScreenGui.
Click Detectors only work when:

.Activated works for any device, however, .MouseButton1Down only works for computer/laptop devices.

Not true at all, works for all devices.