Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script if statement not working even though printing the value is corrent?

Asked by 5 years ago

Hi Guys,

I have a script that will basically check if the player is not on the team named "Passenger". If they are not on it, it will print that they can spawn, whereas if they are on the team it will print that they cant spawn. Could someone help me because all it seems to do is just go to the else part and doesn't ever fulfill the if statement, even though i am 100% sure it does. Script is below, any advice would be great!

script.Parent.Spawn.TextButton.MouseButton1Click:Connect(function()
    print(game.Players.LocalPlayer.Team)
if game.Players.LocalPlayer.Team ~= "Passenger" then
    print("Can spawn bus")
else
    print("Can NOT spawn bus")
end
end)
1
You are comparing a Team instance to a string which will be false each time. If you are checking the name of the team then use .Name to get the string name. User#5423 17 — 5y

1 answer

Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago

What kingdom said is the exact issue you're facing. It's true that printing game.Players.LocalPlayer.Team will return the team's name, but that is actually not a string, it is returning the Instance by showing its name. And yiy are comparing an Instance to a string, and that is obviosuly false. So what you gotta do is get the team's name, which is a string value, and thenn the checking if the team's name is == to "Passenger" will do its thing. Always watch out from this instance/string thing.

script.Parent.Spawn.TextButton.MouseButton1Click:Connect(function()
print(game.Players.LocalPlayer.Team.Name) --you can add a .Name here and you'll see that it'll print the same thing, but without the .Name it will return the instance, with it will return a string.
if game.Players.LocalPlayer.Team.Name ~= "Passenger" then
    print("Can spawn bus")
else
    print("Can NOT spawn bus")
end
end)
0
I dont know how i missed this part :/ 3am scripting isnt the best of time to script xD. It works now, thanks!! MrTomasboy 13 — 5y
0
np! starmaq 1290 — 5y
0
and sorry for begging but can you please upvote my answer? im on a grind! starmaq 1290 — 5y
0
ty a lot! starmaq 1290 — 5y
Ad

Answer this question