Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Need help with Defining the "msg" within a onChatted Command?

Hi, I'm using a IntValue to bind 2 scripts together. The Name of the IntValue is "SpeedValue" and the function of the scripts is to change the Walk speed of Players. 1 script uses onTouch and the other uses onChatted.

Here is Script 1:

local BasePlate = script.Parent
local Speed = game.Workspace.SpeedValue.Value
BasePlate.Touched:connect(function(Brick)
	local Player = Brick.Parent:findFirstChild("Humanoid")
	Player.WalkSpeed = Speed
end)

and Here is Script 2:

local Speed = game.Workspace.SpeedValue.Value
game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        msg.Text = Speed
    end)
end)

The Problem I'm having is that I'm fairly new to onChatted and the Wiki was no help (seriously I read through it like 4 times). I the output displays: "Workspace.Speed Command:4: attempt to index local 'msg' (a string value)"

Thanks.

2 answers

✓ Accepted answer

I'm trying to make it so any number you say in the chat, that number becomes the Walkspeed

If this is the case then why would you need an IntValue, and why are you trying to set Speed? Anyway "msg" is simply the string that the player chatted, it has no Text property. I think the error message is quite clear.

Try using tonumber() like so:

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        player.Character.Humanoid.WalkSpeed = tonumber(msg)
    end)
end)

1 comment

0

Thanks, I'm just messing about with chat Commands and I thought using Speed might a fun way to go about it. I used a Int Value because I thought it would be helpful in connecting 2 scripts.

The way that the Chatted method works is that it gives you the message received from the player as an argument, therefore you are attempting to set the message that the player has said to your value. Script one in your case is mostly correct however you are referring to the Value of the object instantly at the top meaning that no matter when the value changes the Speed variable will be set to the initial value throughout.

local BasePlate 	= 	script.Parent
local Speed 		= 	game.Workspace.SpeedValue

BasePlate.Touched:connect(function(Brick)
	if Brick.Parent:FindFirstChild("Humanoid") ~= nil then
		Player.Humanoid.WalkSpeed 		=	Speed.Value
	end
end)

As you can see the value of the object is put in to the equation when the value is required.

An example of how your Chatted script should be is as follows:

local Speed 		= 	game.Workspace.SpeedValue

game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg)
		if (typeof(tonumber(msg)) == "number") then
			Speed.Value 	=	tonumber(msg)
		end
	end)
end)

Given that a message will be a string once it is passed back we must convert it to a number and double check that it is then in-fact a number before setting the IntValue to the variable.

1 comment

0

I'm trying to make it so any number you say in the chat, that number becomes the Walkspeed. Sorry I wasn't clear enough in the Desc.

This discussion is preserved as it was. New answers, comments, and votes are closed.