I did it first on me, then I tried to do it on another player, but it kept fire at me instead.
Oh, here is the problem:
This
local character = player.Character
should be
local character = PlayerObject.Character
Edit:
(player
is the player who chatted, PlayerObject
is the player who the cmd was meant for)
PlayerObject is defined here:
local PlayerObject = Players:FindFirstChild(playerName)
Which means PlayerObject
is the player with the name given in the cmd.
What is Player Object? and why should it be changed to that?
OH I SEE
so basically, when I put player, since player was defined as the person who sent the message, thats why the effects keep going on them?
And also, if I wanted to make more commands, would I use elseif or else ( enter ) if?
Yep! So all you need to do is switch it to the variable that represents the other player.
I’d use elseif, though both do the same thing (elseif looks nicer and is cleaner though).
Can you possibly give me an example for what it would look like If I made 2 commands with the else if? because I don’t know where to start with that.
I removed the cmd list thing, normally that table also stores functions for each command. It’s probably just easier to do the commands with if and elseif statements
local Prefix = "+"
local RS = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if player.Name == "NubblyFry" then
local Players = game:GetService("Players")
local commandMessage = message
-- Get the first character of the message
local commandCharacter = string.sub(commandMessage, 1, 1)
-- Is cmd character valid?
if commandCharacter == "+" then
-- Gets the list of words in commandMessage. sub is used to ignore cmd character
local commandDetails = string.split(string.sub(commandMessage, 2, -1), " ")
-- Name of command
local commandName = commandDetails[1]
-- Name of player
local playerName = commandDetails[2]
-- Is name of player valid?
if Players:FindFirstChild(playerName) then
if commandName == "smite" then
-- Code for smite
elseif commandName == "kill" then
-- Code for kill or other cmd
elseif commandName == "fling" then
-- Code for fling or other cmd
end
-- If not a command with code does nothing
end
end
end
end)
end)
One last question, is it possible to delete the chat message? I want to send the chat message commands in the chat and then have it deleted by the system but still run.
Yeah. You can use the process command api for that.
This is the format for the process command stuff:
In a ModuleScript:
local ChatService = game:GetService("Chat")
local functionId = "customCommandSystem"
-- If this function returns true, the message is removed.
local function processCommand(speakerName, message, channelName)
local speaker = ChatService:GetSpeaker(speakerName)
local player = speaker:GetPlayer()
if player.Name == "NubblyFry" then
local Players = game:GetService("Players")
local commandMessage = message
-- Get the first character of the message
local commandCharacter = string.sub(commandMessage, 1, 1)
-- Is cmd character valid?
if commandCharacter == "+" then
-- Gets the list of words in commandMessage. sub is used to ignore cmd character
local commandDetails = string.split(string.sub(commandMessage, 2, -1), " ")
-- Name of command
local commandName = commandDetails[1]
-- Name of player
local playerName = commandDetails[2]
-- Is name of player valid?
if Players:FindFirstChild(playerName) then
if commandName == "smite" then
-- Code for smite
return true -- If fling command remove message
elseif commandName == "kill" then
-- Code for kill or other cmd
return true -- If fling command remove message
elseif commandName == "fling" then
-- Code for fling or other cmd
return true -- If fling command remove message
end
-- If not a command with code does nothing
return false -- If not a command doesn't remove message
end
end
end
end
local function runChatModule(ChatService)
ChatService:RegisterProcessCommandsFunction(functionId, processCommand)
end
return runChatModule
Then put the module script inside a server script, and put this code into the server script:
local ChatService = game:GetService("Chat")
local moduleScript = script:FindFirstChildOfClass("ModuleScript")
-- This is where the module script needs to be placed
moduleScript.Parent = ChatService:WaitForChild("ChatModules")
Note with this the code can’t yield, so you should use task.spawn to stop your command’s code from yielding:
Ex:
task.spawn(function()
local SmiteBlock = RS:WaitForChild("commandObjects"):WaitForChild("Smite")
local PlayerObject = Players:FindFirstChild(playerName)
local character = player.Character
local ThunderSound = Instance.new("Sound", game.Workspace)
ThunderSound.SoundId = "rbxassetid://357559831"
ThunderSound:Play()
SmiteBlock.Position = character:WaitForChild("HumanoidRootPart").Position
task.wait(0.2)
SmiteBlock:Destroy()
end)