hey guys i have been coding a game called america simulator and i am having trouble with my script:
part = script.Parent
part.Touched:connect(function(SwitchTeams)
print(“Switching Teams…”)
local PlayerTeam = game.Players:GetPlayerFromCharacter(SwitchTeams.Parent).Team
if PlayerTeam == game.Teams.red then
PlayerTeam = game.Teams.blue
print(“switched teams”)
end
end)
The code is supposed to do the following:
player touches the part:
if the player is on the red team they switch to the blue team
if they are on a team that isnt red they will not change teams because i dont want just any team switching to blue.
my problem is there are no error messages and all of the messages are printing still, so there is a problem i just dont know where the problem is.
So far i have tried:
making sure the caps and lowercase are correct,
changing the name of the teams,
and changing the name of the function.
NOTHING WORKS CAN SOMEONE PLEASE HELP ME
Try this:
part = script.Parent
part.Touched:connect(function(SwitchTeams)
if SwitchTeams.Parent:FindFirstChild("Humanoid") then
local PlayerTeam = game.Players:GetPlayerFromCharacter(SwitchTeams.Parent)
if PlayerTeam.Team == game.Teams.red then
PlayerTeam.Team = game.Teams.blue
end
end
end)
I just removed .Team
from your PlayerTeam variable. I also added a check for Humanoid, but you could probably change it to something different if you would like. Also I recommend opening the output window under the view tab, cause it could have been your script erroring too.
The mistake you’re making is that you set the variable to get the Team. And uhhh, it is its own value so if you change that variable into something, Player.Team won’t get changed.
thank you for such a fast reply i will try this and report back to you if it works.
oh ok thank you for your help as well
thank you guys very much for your help, the script you gave me works! im glad i could learn for this problem and its solution.
Mark Tyler’s post as the solution pls.
ok sure thing
(text added so i can post this reply)
oh ye i accedently marked the worng solution
also i have another thing its not really a problem but it would be nice if someone could help me with this:
so i made the script on this topic cuz i was having a problem and this reply is related to that script.
i was wondering if i could make it go from one part to all exept for one par, why? because lemme give you some context:
in another game i want it to be if someone is put in jail by a police officer it makes them the prisoner team (which i already scripted) but i want it to be where if the player leaves the jail they become a team like Criminal or Escaper, or in other words, if the player is not touching the jail then they become criminal team.
You can use the .TouchEnded event on the part that fires when another part is, you guessed it, not touching the part anymore.
ok, but can you please show me where i put the .TouchEnded?
also i havnt exactly added any scripts to the jail, instead i simply put a team changing spawnlocation, should i remove this so it doesnt cause problems with the script?
You put .TouchEnded the same way you put .Touched.
ok i spent a bit of time and did you mean like this?
part = script.Parent
part.TouchEnded:connect(function(SwitchTeams)
if SwitchTeams.Parent:FindFirstChild(“Humanoid”) then
local PlayerTeam = game.Players:GetPlayerFromCharacter(SwitchTeams.Parent)
if PlayerTeam.Team == game.Teams.red then
PlayerTeam.Team = game.Teams.blue
end
end
end)
Yup. Also this is off-topic but next time you want to show your snippets. Use the backtick symbols “```” and wrap them into the snippet so your code would look more readable. And put lua in the first three backticks for syntax highlighting to look a bit nicer.
ok thank you this will help with my game alot as well as my future posts
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.