Why isn't this script working?

Hey uhh, I’m trying to make a Sans vs Chara game, and basically, if you join the game first, you’ll automatically be put on the Sans team. And if you’re on the Sans team, your body parts are supposed to go invis. However, that doesn’t happen. What’s wrong with this thing?

  1. What do you want to achieve? Player goes invisible if on Sans team.

  2. What is the issue? Player doesn’t go invisible. Yet in the output, it shows it’s working.

  3. What solutions have you tried so far? Tried tweaking the code, same result. Here’s the code.

local Players = game.Players or game:GetService("Players")
local Teams = game.Teams
local Sans = Teams.Sans
local Chara = Teams.Chara


Players.PlayerAdded:Connect(function(Player)
	if Sans.Taken.Value == false and Chara.Taken.Value == true or Chara.Taken.Value == false then
		Player.RespawnLocation = game.Workspace["Sans Spawn"]
		Player.Team = Sans
		print(Player.Name.." is on the Sans team. Imagine joining first and then pressing fn+f9.")
		Player.CharacterAdded:Connect(function(Character)
			print(Character.Name.." added to workspace!")
			local Children = Character:GetChildren()
			for i = 1, #Children do
				if Children[i].ClassName == "MeshPart" then
					local MeshParts = Children[i]
					MeshParts.Transparency = 1
					print(MeshParts.Name..tostring(MeshParts.Transparency))
				end
			end
		--	Character:Remove()
		end)
		Sans.Taken.Value = true
	elseif Sans.Taken.Value == true and Chara.Taken.Value == false then
		Player.RespawnLocation = game.Workspace["Chara Spawn"]
		Player.Team = Chara
		print(Player.Name.." is on the Chara team. Imagine joining first and then pressing fn+f9.")
	end
end)

Removing the character just broke the free cam. So, I scrapped that.

2 Likes

Try changing MeshPart to BasePart, or fuse them together, becaus if the player doesn’t have only MeshParts he will not go invisivle

3 Likes

Most of the things in this code are actually bogus / useless.

You don’t have to manually set the team when the player joins, because it automatically sets a team itself, and distributes the players equally. Just set both team’s AutoAssignable property to true. Of course if the “Sans” doesn’t set first, you’ll just have to do it manually.

Also, I notice how you manually set the player’s RespawnLocation to an Instance. I don’t know if that’s a SpawnLocation, but if it is, it’s not necessary. Player automatically spawn at their own team’s SpawnLocations.

Another thing, although this may be slightly advanced.

local Children = Character:GetChildren()
for i = 1, #Children do
	if Children[i].ClassName == "MeshPart" then
		local MeshParts = Children[i]
		MeshParts.Transparency = 1
		print(MeshParts.Name..tostring(MeshParts.Transparency))
	end
end

You can use a special loop meant for looping through tables, like what :GetChildren() returns. It’s called a for in loop.

Here’s it’s syntax:

for keyOrIndex, value in table do
    print(value) -- this will run for every item in the table.
end

So using this loop, your code may look like this:

for i, child in Character:GetChildren() do
	if child.ClassName == "MeshPart" then
		local MeshParts = child
		MeshParts.Transparency = 1
		print(MeshParts.Name..tostring(MeshParts.Transparency))
	end
end
2 Likes
if Children[i]:IsA("MeshPart") then

tostring is not necessary, Numbers concatenated to strings will automatically make them a string

print(MeshParts.Name..MeshParts.Transparency)
2 Likes

You’re responding to me, but I didn’t write the script. I think you should respond to him instead, because the only thing I corrected / edited was the loop.

You’re right though, there are a lot of nit-picks in this code, and I could go for hours.

1 Like

the first thing I saw was this script.

1 Like

This is just blatantly stupid, and thinking performance wise, a waste of CPU time.

You don’t have to redefine a new variable with a different identifier name just because you’ve just verified it’s type. Just continue on using the child identifier.

1 Like

Also, I don’t agree that’s it’s NOT necessary. I just don’t. tostring is always a fallback method to make sure something is string.

1 Like

Regular Lua already does this by itself which I don’t see how it would fail, when doing this, Lua assumes its a string and vice versa, like this:

print("10" + 1) --> 11
print(10 .. 12) --> 1012
print("10" .. 12)
1 Like

Are you saying when exclusively using the print function? Or, generally? Because if the not the latter, it’s still a safe method. If you were storing a concatenation of a number, or some other arbitrary type, it would still be verifying that it has to be a string.

Try this code, so when the CharacterAdded event is fired the transparent code has already been executed

local Players = game.Players or game:GetService("Players")
local Teams = game.Teams
local Sans = Teams.Sans
local Chara = Teams.Chara

Players.PlayerAdded:Connect(function(Player)
    if Sans.Taken.Value == false and Chara.Taken.Value == true or Chara.Taken.Value == false then
        Player.RespawnLocation = game.Workspace["Sans Spawn"]
        Player.CharacterAdded:Connect(function(Character)
            local Children = Character:GetChildren()
            for i = 1, #Children do
                if Children[i].ClassName == "MeshPart" then
                    local MeshParts = Children[i]
                    MeshParts.Transparency = 1
                end
            end
        end)
        Player.Team = Sans
        print(Player.Name.." is on the Sans team. Imagine joining first and then pressing fn+f9.")
        Sans.Taken.Value = true
    elseif Sans.Taken.Value == true and Chara.Taken.Value == false then
        Player.RespawnLocation = game.Workspace["Chara Spawn"]
        Player.Team = Chara
        print(Player.Name.." is on the Chara team. Imagine joining first and then pressing fn+f9.")
    end
end)

Ehh, unfortunately that didn’t work.

This also didn’t work, the player can still see their character.

And… this too didn’t work either. The output is showing that it’s working by sending me the prints, but the server isn’t registering the transparency change I guess.

I think the best way to handle this is to make them invisible through the Client, for example whenever for each character and whenever a character is added the client will make them invisible.

you can try using runservice and teleporting them for where they’re hidden except for the LocalPlayer, teleporting other players through LocalScript will not effect them.

You’re trying to make the player invisible? Here’s how I would do it:

local transparency = .9 -- the amount of transparency

for _, v in char:GetChildren do
    if v:IsA("Accessory") then 
        v.Handle.Transparency = transparency
    elseif v:IsA("BasePart") then
        v.Transparency = transparency
    end
end

This should be the basic format for doing anything related physical objects on the character.

This actually worked. Thank you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.