I am making it so if you own a gamepass and you click a button, you can go on a special team, but it is not working with no errors.
Script:
function Click(mouse)
local gamepassID = 9597486
if script.Parent.Parent.Parent.Parent.Parent:IsInGroup(6068826) and script.Parent.Parent.Parent.Parent.Parent:UserOwnsGamePassAsync(userID, gamepassID) then
script.Parent.Parent.Parent.Parent.Parent.TeamColor = BrickColor.new("Daisy orange")
end
end
script.Parent.MouseButton1Click:connect(Click)
function Click(mouse)
local Player = game.Players.LocalPlayer
local gamepassID = 9597486
if Player:IsInGroup(6068826) and Player:UserOwnsGamePassAsync(Player.UserId, gamepassID) then
Player.TeamColor = BrickColor.new("Daisy orange")
end
end
script.Parent.MouseButton1Click:Connect(Click)
I’ve been away from Roblox until recently due to college, so I’m refreshing my brain.
Your use of a LocalScript may be the issue. This is because the tasks you are doing (such as checking for an asset) are taken care of on Roblox’s servers, so it may not work in a LocalScript, though I have not tried. Try this code inside a regular Script (which is a server script).
game.Players.PlayerAdded:Connect(function(Player)
--local Player = game.Players.LocalPlayer <<< can't use this in a server script
local gamepassID = 9597486
if Player:IsInGroup(6068826) and Player:UserOwnsGamePassAsync(Player.UserId, gamepassID) then
Player.TeamColor = BrickColor.new("Daisy orange")
end
end)
function Click(mouse)
game.Players.PlayerAdded:Connect(function(Player)
local gamepassID = 9597486
if Player:IsInGroup(6068826) and Player:UserOwnsGamePassAsync(Player.UserId, gamepassID) then
Player.TeamColor = BrickColor.new("Daisy orange")
end
end)
end
script.Parent.MouseButton1Click:Connect(Click)
so that it does that when the gui button is clicked?
Yes, I just wanted to see if my code would fix it. Did it work?
To make the click work, use a remote event. Look at the Roblox webpage about it if you are not familiar with them. With the remove event, it can be arranged so that when the player clicks the button, the client sends a request to the server, and the server does whatever it needs to do.
It would be insecure if the client side had the authority to put you in teams.
This is why you need to use a remote event or remote function.
Something like this would do what you’re looking for
-- LocalScript (inside the button Ui)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local requestTeamChangeEvent = ReplicatedStorage:WaitForChild("requestTeamChangeEvent")
-- When you press the button, fire that specific event
function Click(mouse)
requestTeamChangeEvent:FireServer()
end
script.Parent.MouseButton1Click:connect(Click)
-- Script (inside ServerScriptService)
local gamepassID = 9597486
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local requestTeamChangeEvent = Instance.new("RemoteEvent", ReplicatedStorage)
requestTeamChangeEvent.Name = "requestTeamChangeEvent"
-- When this specific event is fired, switch the player's team
local function onCreatePartFired(player)
print(player.Name, "wants to switch teams!")
if player:IsInGroup(6068826) and player:UserOwnsGamePassAsync(userID, gamepassID) then
player.TeamColor = BrickColor.new("Daisy orange")
end
end
requestTeamChangeEvent.OnServerEvent:Connect(onCreatePartFired)