I have a script, where I want to set the value to be any number between 2 integers. How would I exactly go about with this?
My script looks something like this
local timevalue = timevalue >= 10 and timevalue <= 720 game.Players.PlayerAdded:connect(function(player) player.Chatted:connect(function(msg) if msg ==(".timeset " .. timevalue) then print(timevalue) end end) end)
Nothing is printing, and there is no error being shown in the output.
Noted : Just in case this has some strange thing to do with it, which I doubt, but just want to give you all the information, I am using a Localscript in Workspace.
Thanks, FravLs
The reason nothing's printing is because your chat event isn't established with the local player. You're waiting for PlayerAdded
, which will fire when other players join the game, but not yourself. You've already loaded in by the time this code is running.
You should separate the code for that event into a function, so you can apply it to yourself and other players when they join the game. Reusable code is happy code.
local Players = game:GetService("Players") local function PlayerJoined(player) -- end PlayerJoined(Players.LocalPlayer) Players.PlayerAdded:Connect(PlayerJoined)
There's a small logic problem with your code, that has to due with your timevalue
variable. If I understand you correctly, you want the user to be able to chat .timeset
followed by any range of numbers you give it. Right now, you're using comparative operations such as greater than or equal too and less than or equal too. These will return booleans, not numbers.
What you want do it, is get the user's message first, then analyze the numbers in their message string.
local Players = game:GetService("Players") local function PlayerJoined(player) player.Chatted:Connect(function(message) -- Compare characters 1-9 with command if message:sub(1, 9) == ".timeset " then -- Get characters after 9th, convert to a number, and assess local timevalue = tonumber(message:sub(10)) if timevalue and timevalue >= 10 and timevalue <= 720 then print(timevalue) end end end) end PlayerJoined(Players.LocalPlayer) Players.PlayerAdded:Connect(PlayerJoined)
That's all there is to it, hope this helped, let me know if you have any questions.
local timevalue = math.random(10,720) game.Players.PlayerAdded:connect(function(player) player.Chatted:connect(function(msg) if msg ==(".timeset " .. timevalue) then print(timevalue) end end) end)