Hello, developers! So I had taken a script from one of @ItsKoolPlayz’s posts, and tweaked it to be a command in which if you are a certain team, you will be able to use a command to teleport to a spot. The problem is, it doesn’t work, with no errors. Please help me! Code:
local prefix = ":"
local teleportCommand = "to"
local hostTeleport = game.Workspace.HostTeleport
local coHostTeleport = game.Workspace.CoHostTeleport
game:GetService("Players").PlayerAdded:Connect(function(Player)
if (Player.TeamColor == BrickColor.new("Bright bluish green") or Player.TeamColor == BrickColor.new("Slime green")) then
Player.Chatted:Connect(function(Message)
local prefixLength = string.len(prefix)
if (string.sub(Message, prefixLength + 1)) then
local command = string.sub(Message, prefixLength + 1)
local splitBySpaces = string.split(command, " ")
if (splitBySpaces[1] == teleportCommand) and (splitBySpaces[2]) then
if (string.len(splitBySpaces[2]) == "host" and Player.TeamColor == BrickColor.new("Bright bluish green")) then
local playerToTeleport = Player
playerToTeleport.Character:FindFirstChild("HumanoidRootPart").CFame = hostTeleport.CFrame
elseif (string.len(splitBySpaces[2]) == "cohost" and Player.TeamColor == BrickColor.new("Slime green")) then
local playerToTeleport = Player
playerToTeleport.Character:FindFirstChild("HumanoidRootPart").CFame = coHostTeleport.CFrame
end
end
end
end)
end
end)
I see the issue. As mentioned above there were some typos. But there is something else as well, the issue lies where you are comparing the strings to the commands in the if statements.
In the above if statement, you are trying to compare the input with the command. but you are taking the length of the string, not the string itself. Here is what the whole thing should be:
local prefix = ":"
local teleportCommand = "to"
local hostTeleport = game.Workspace.HostTeleport
local coHostTeleport = game.Workspace.CoHostTeleport
game:GetService("Players").PlayerAdded:Connect(function(Player)
if (Player.TeamColor == BrickColor.new("Bright bluish green") or Player.TeamColor == BrickColor.new("Slime green")) then
print("Initiated")
Player.Chatted:Connect(function(Message)
print("Chatted")
local prefixLength = string.len(prefix)
if (string.sub(Message, prefixLength + 1)) then
print("Chatted big enough")
local command = string.sub(Message, prefixLength + 1)
local splitBySpaces = string.split(command, " ")
print("Command: " .. splitBySpaces[1], splitBySpaces[2])
if (splitBySpaces[1] == teleportCommand) and (splitBySpaces[2]) then
if (splitBySpaces[2] == "host" and Player.TeamColor == BrickColor.new("Bright bluish green")) then
local playerToTeleport = Player
playerToTeleport.Character:FindFirstChild("HumanoidRootPart").CFrame = hostTeleport.CFrame
elseif (splitBySpaces[2] == "cohost" and Player.TeamColor == BrickColor.new("Slime green")) then
local playerToTeleport = Player
playerToTeleport.Character:FindFirstChild("HumanoidRootPart").CFrame = coHostTeleport.CFrame
end
end
end
end)
end
end)
did you swap to the team before the script checked?
You will need someway was check what team they are on.
Here is where the problem is:
game:GetService("Players").PlayerAdded:Connect(function(Player)
if (Player.TeamColor == BrickColor.new("Bright bluish green") or Player.TeamColor == BrickColor.new("Slime green")) then
change that, to this:
game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message)
if (Player.TeamColor == BrickColor.new("Bright bluish green") or Player.TeamColor == BrickColor.new("Slime green")) then
It was a combination of you two’s work, so therefore I’m not marking anyone as solution, it was a team effort. I will be adding solution to the title though! Thank you!
local hostTeleport = workspace.HostTeleport
local coHostTeleport = workspace.CoHostTeleport
local colors = {BrickColor.new("Bright bluish green"), BrickColor.new("Slime green")}
game.Players.PlayerAdded:Connect(function(Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
Player.Chatted:Connect(function(Msg)
if string.match(Msg, "^:") then
local Command = string.sub(Msg, ":")
local SplitMsg = string.split(Command, " ")
if (SplitMsg[1] == "to") and (SplitMsg[2]) then
if (SplitMsg[2] == "host" and Player.TeamColor == colors[1]) then
HRP.CFrame = hostTeleport.CFrame
elseif (SplitMsg[2] == "cohost" and Player.TeamColor == colors[2]) then
HRP.CFrame = coHostTeleport.CFrame
end
end
end
end)
end)