Problems with teleporting with the "Chatted" funcion

Solution found, look at the feature in-game: https://www.roblox.com/games/4785430023/the-hangout-desc-pls

Okay so, I am trying to make a funcion that teleports you whenever you say: “/tower” it will teleport the user to the part that is near the tower i made in my game, the tower is irrelevant but for more context.
Here is the code I made

local player = game.Players.LocalPlayer
local endpart = workspace.Part

game.Players.PlayerAdded:Connect(function()
	player.Chatted:connect(function(msg)
		msg = msg.lower()
		if msg == "/tower" then
			player.HumanoidRootPart.Position = endpart.Position
		end
	end)
end)

And there was a error, here: image
So if you are a experienced Scripter and know how to fix this, leave a reply with the solution. Thanks!
PS: I titled myself a builder, since I am still practising coding, and not a really good one yet.

Line 5, connect is meant to be a capital c,
player.Chatted:Connect(function(msg)

As well, you need to define player.

game.Players.PlayerAdded:Connect(function(player)

    -- put your code here

end)

You never defined player in your functons.

game.Players.PlayerAdded:Connect(function(player)
    --Code Here
end)
1 Like

You must pass the player argument in the function

Also, you need to do:

player.Character.HumanoidRootPart.Position

not

player.HumanoidRootPart.Position

First you need to pass the player as one of the arguments for the chatted event to recognize who even chatted, Second of all you need to get the character to call any child of that player’s character

for instance:

player.Character:WaitForChild("Torso")

Also, I’d suggest getting used to :Connect() as it looks more neat

I’d suggest using CFrame for a better a outcome

player.Character.HumanoidRootPart.CFrame = CFrame.new(workspace:FindFirstChild("Part Here").Position)

Thanks for all your help, but now it has a problem with this:

msg = msg.lower()

Simple, You’re calling the variable the same name as the argument passed in the event simply change the name of the variable to something else like LowerMessage and such, Hopefully I helped if not reply to this and I’ll get back as soon as possible

Message = msg.lower()

Please, don’t violate camel case, it hurts my eyes.

I don’t want to come off as rude, it’s just a bad practice to name variables starting with capital letters.

1 Like

So you want me to put this?

player.Chatted:Connect(function(msg)
		Message = msg.lower()
end)

My bad I guess its a bad habit of mine. :sweat_smile:

This might seem like “SpoonFeeding” but I’m not the best with explaining so here you go

local endpart = workspace.Part

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		local loweredMessage = msg.lower()
		if loweredMessage == "/tower" then
			player.Character.HumanoidRootPart.CFrame = CFrame.new(endpart.Position)
		end
	end)
end)

lowerMessage is a unknown Global

Didnt notice that here I edited my reply recheck.

image
basically, that line is the lowerMessage variable

I don’t understand your problem can you send the line that the error pops up at.

image

1 Like

Lol, I don’t see anyone doing this right.

This should work:

local endpart = workspace.Part

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		local tpmessage = string.lower("/tower")
		if msg == tpmessage then
			player.Character.HumanoidRootPart.CFrame = endpart.CFrame
		end
	end)
end)

Edit: you should probably use CFrames

tpmessage is a unknown global :smiley:

You forgot to add the player argument.