The title says it all! I looked up “MouseButton1Click” to see if I could find a solution, but I didn’t find anything
Connect it to a function with the first parameter as player.
For example:
button.MouseButton1Click:Connect(function(player)
print(player) -- it will print the players name.
end)
Well I tried this and it didn’t work, and I already set up the teams
''''
local D = game.Teams:WaitForChild("D")
script.Parent.MouseButton1Click:Connect(function(player)
player.Team = D.Team
end)
'''
Serverscript
You have to manually define the player. GUI scripts are most often done on a local script that can access the local player with ease which is why you must define it.
local player = game.Players.LocalPlayer
I think you mistake is that you said D.Team, but i might be wrong, any errors?
''''
local Class = game.Teams:WaitForChild("Class")
script.Parent.MouseButton1Click:Connect(function(player)
player.Team = Class
end)
'''
Serverscript
This worked for me:
local Team = game.Teams:WaitForChild("Team")
script.Parent.ClickDetector.MouseClick:Connect(function(player)
game.Players[player.Name].Team = Team
end)
This is why it is imperative that you search for similar posts before posting, or look at the DeveloperHub, the event just doesn’t pass the player as an argument so this:
won’t work either.
MouseButton events are replicated across the client-server boundary, you can bind functions to them from a server-script if you manage to get the GuiObject - though it is advised to just handle this stuff locally, that way it’ll be even easier
-- LocalScript
local player = game.Players.LocalPlayer
local object = script.Parent -- Gui Button Object
local function onClicked()
print(player.Name)
end
-- Object.Activated:Connect(onClicked) -- MouseButton events might be deprecated in the future, if you don't need to listen for such input through the server, use Activated over MouseButton1Click
Object.MouseButton1Click:Connect(onClicked)
I typed that, but after that i fixed my mistake and made it work and i’m also very new to scripting.
Well since it is in a local script, other players don’t know you are on that team, how can I fix that?
By using my script:
local Team = game.Teams:WaitForChild("Team")
script.Parent.ClickDetector.MouseClick:Connect(function(player)
game.Players[player.Name].Team = Team
end)
Server Script inside part
Players.Kamlkaze_Kid.PlayerGui.ScreenGui.Frame.Scientific Department.Script:4: attempt to index nil with ‘Name’ I edited your script
local Team = game.Teams:WaitForChild("Scientific Department")
script.Parent.MouseButton1Click:Connect(function(player)
game.Players[player.Name].Team = Team
end)
Oh, i didn’t know it was in a gui, let me fix that.
First add a remote event to replicated storage:
Local Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
script.Parent.TextButton.MouseButton1Click:Connect(function()
RemoteEvent:FireServer()--Fires remote event
end)
Server Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local Team = game.Teams:WaitForChild("Team") -- Change this to the name of your team.
RemoteEvent.OnServerEvent:Connect(function(player)--If the remote event is received
game.Players[player.Name].Team = Team
end)
I hope this helps!
Where do I put the scripts???
Put it in ServerScriptService or workspace but i suggest ServerScriptService because it’s a nice place to put scripts and only scripts.
What if there is no team in the game?