Script help.team change more then once

local MarketplaceService = game:GetService(“MarketplaceService”)

local Player = script.Parent.Parent.Parentlocal GamePassId = 162872866

local HasPass = false

local success, message = pcall(function()

HasPass = MarketplaceService:UserOwnsGamePassAsync(Player.UserId,GamePassId)

print(HasPass)

end)

if HasPass == true then

Player.Team = game.Teams.Imam

end

local Opened = false

local Teams = game:GetService(“Teams”)

script.Parent.Team.MouseButton1Click:Connect(function()

if Opened == false then

script.Parent.Frame:TweenPosition(UDim2.new(0.236, 0,0.266, 0))

Opened = true

else

script.Parent.Frame:TweenPosition(UDim2.new(0.236, 0,-0.9, 0))

Opened = false

end

end)

script.Parent.Frame.Imam.MouseButton1Click:Connect(function()

if HasPass == true then

script.Parent.Parent.Parent.Team = Teams.Imam

script.Parent.Parent.Parent.Team.TeamColor = Teams.Imam.TeamColor

wait()

script.Parent.Parent.Parent:LoadCharacter()

end

end)

script.Parent.Frame.Normal.MouseButton1Click:Connect(function()

script.Parent.Parent.Parent.Team = Teams.Normal

script.Parent.Parent.Parent.Team.TeamColor = Teams.Normal.TeamColor

wait()

script.Parent.Parent.Parent:LoadCharacter()

end)

If the player can only be spawned in as Imam(team) if the player changes his team to Normal he can not change it back. How do I fix this? MODEL ID:13132641394

Remember to format the code you paste in DevForum, by using Control+E or the button
image

You cant call LoadCharacter() from client side.
The colors of the teams are different since the start?
If you are on client side, no need to do parent.parent.parent etc to get the player, you already have it from the start of the script.
I edited it a little check it:

local MarketplaceService = game:GetService("MarketplaceService")
local Player = game.Players.LocalPlayer

local GamePassId = 162872866

local HasPass = true

local Teams = game:GetService("Teams")

-- Colors should be different since the start
warn("Imam Color is:", Teams.Imam.TeamColor)
warn("Normal Color is:", Teams.Normal.TeamColor)


local success, message = pcall(function()
	HasPass = MarketplaceService:UserOwnsGamePassAsync(Player.UserId,GamePassId)
	warn(Player,"has the pass?:",HasPass)
end)


if HasPass then
	Player.Team = Teams.Normal -- starter team should be Normal I think
end

local Opened = false

script.Parent.Team.MouseButton1Click:Connect(function()
	if Opened == false then
		script.Parent.Frame:TweenPosition(UDim2.new(0.236, 0,0.266, 0))
		Opened = true
	else
		script.Parent.Frame:TweenPosition(UDim2.new(0.236, 0,-0.9, 0))
		Opened = false
	end
end)

script.Parent.Frame.Imam.MouseButton1Click:Connect(function()
	if HasPass then
		warn("team change to Imam")
		Player.Team = Teams.Imam
		wait()
		--Player:LoadCharacter() -- cant be called on client side
	end
end)

script.Parent.Frame.Normal.MouseButton1Click:Connect(function()
	warn("team change to Normal")
	Player.Team = Teams.Normal
	wait()
	--Player:LoadCharacter() -- cant be called on client side
end)

so u mean i put this in a local script?

No, you don’t want this code in a local script as it checks for if they have the gamepass and such. If in a local script that could be exploited. Also, he means you can’t call LoadCharacter from a local script (aka the client side)

so i just copy and past the code he provided. Wouldn’t this work in a local scrpt look at other code I just send bellow this

local MarketplaceService = game:GetService("MarketplaceService")

local Player = script.Parent.Parent.Parent
local GamePassId = 162872866
local HasPass = false
local success, message = pcall(function()
	HasPass = MarketplaceService:UserOwnsGamePassAsync(Player.UserId,GamePassId)
	print(HasPass)
end)
if HasPass == true then
	Player.Team = game.Teams.Imam
end
local Opened = false
local Teams = game:GetService("Teams")

script.Parent.Team.MouseButton1Click:Connect(function()
	if Opened == false then
		script.Parent.Frame:TweenPosition(UDim2.new(0.236, 0,0.266, 0))
		Opened = true
	else
		script.Parent.Frame:TweenPosition(UDim2.new(0.236, 0,-0.9, 0))
		Opened = false
	end
end)

script.Parent.Frame.Imam.MouseButton1Click:Connect(function()
	if HasPass == true then
		script.Parent.Parent.Parent.Team = Teams.Imam
		script.Parent.Parent.Parent.Team.TeamColor = Teams.Imam.TeamColor
		wait()
		script.Parent.Parent.Parent:LoadCharacter()
	end
end)

script.Parent.Frame.Normal.MouseButton1Click:Connect(function()
	script.Parent.Parent.Parent.Team = Teams.Normal
	script.Parent.Parent.Parent.Team.TeamColor = Teams.Normal.TeamColor
	wait()
	script.Parent.Parent.Parent:LoadCharacter()
end)

I mean, your code is client sided. And theres stuff doesnt work on client side, plus the lot of parent check.

You should check the gamepass on server, on player join. And use remote when player uses the buttons to check on server if they can change team or not

I agree with Dev_Peashie. You need a script on the Server that is checking if they have the pass and assigning the teams.

And in the local script the only thing the mousebutton1click should do is fire a remoteEvent to the server with the team name they want to change to as the parameter.

The server will then check if they have the pass and are allowed to change to the team as defined in the parameter

1 Like

idk know how to do that loll. But how would I do thyat if i wanted too?

I’d watch some tutorials on using Remote Events to start! Personally I think learning it the hard way is going to be more beneficial for you down the line.

2 Likes

I thought remote events were bad.

Remote Events are essential. It’s how you handle the information perceived between the client and server that could be bad. Remote Events are a gateway for the client and server to communicate.

For example, if you have a shop and someone buys something with in game currency. You make a local script that handles the player clicking buy, then you send the information of what was bought to the server and the server then checks if the player has enough money and takes away the money and gives them the item.

If you don’t use a server script for this purchase then when you give them the item ONLY the player who bought the item would see it. ALSO it’s exploitable because the player can exploit it and make the script think they have enough coins when they don’t.

Learning how to use remote events to communicate between the client and server are VERY important.

Keep in mind:

When you start using remote events and in this example someone is hitting a buy button, you want the server script to check if they have the money, not the local script.

I 1000% recommend watching videos and getting a really good understanding on remote events and how the client and server communicate

1 Like

I’m assuming this is a server script. If not, make it one and there should be no issue.

So, why are you doing script.Parent here when you can do Player?

It won’t work. This is not a script.

This script needs to be a server script. Does it work when it’s a server script?