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)
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)
``
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)
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.