Hello! I am having a strange issue, I am making a hand to script (chat based) for a café game and have this bug where it works perfectly while testing alone but in a local test server with two players it does not work, here is my code.
local commands = {}
local prefix = ":"
local function findPlayer(name)
for i, player in pairs(game.Players:GetPlayers()) do
if string.lower(player.Name) == name then
return player
end
end
return nil
end
commands.handto = function(player, arguments)
for i, playerName in pairs(arguments) do
print(playerName)
end
local playerGivingTo = arguments[1]
if playerGivingTo then
local plrGivingTo = findPlayer(playerGivingTo)
if playerGivingTo then
player.PlayerGivingToValue.Value = playerGivingTo
game.ReplicatedStorage.GivePlayerItemEvent:FireClient(player)
end
end
end
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message,recipient)
message = string.lower(message)
local splitString = message:split(" ")
local slashCommand = splitString[1]
local cmd = slashCommand:split(prefix)
local cmdName = cmd[2]
if commands[cmdName] then
local arguments = {}
for i = 2, #splitString, 1 do
table.insert(arguments, splitString[i])
end
commands[cmdName](player, arguments)
end
end)
end)
That script detects if the player says “:handto [Player name]” and then splits it apart to find the player name, and then sets that value to a value I made inside of the player which then gets passed to a local script inside of StarterPlayerScripts, here is that code:
game.ReplicatedStorage.GivePlayerItemEvent.OnClientEvent:Connect(function(player)
local value = game.Players.LocalPlayer.PlayerGivingToValue
local playerToGive = game.Players:FindFirstChild(value.Value)
local toolToGive = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool")
print(playerToGive)
end)
So what happens is that when playing by myself it will print the correct thing, but when playing in the local test server with two fake players it prints nil. Can someone help me? Thanks!
I have thought of some things you could do that may end up in the solution of your problem.
Try changing your game to playable for any friend or your alt and tell them to join so you can test it in an actual player.
Of course if the above thought doesn’t work let me know, so I can re-write a hand-to give command by myself and send it. (with the form “:handto [Player name]”)
Wait do you want me to add a specific group and a specific rank you need to be in and have so the hand-to can work because since I’m working on this I could add this feature but it would delay a little. ( 30m - 1h )
Prefix = ":",
Group = 8676742,
Rank = 4,
}
function GetWords(Msg,Pattern)
local Words = {}
for w in string.gmatch(Msg, Pattern) do
table.insert(Words,w)
end
return Words
end
local ChatFunctions = {
["handto"] = function(Words,Player)
for _,Target in pairs(game.Players:GetPlayers()) do
if string.find(string.lower(Target.Name),string.lower(Words[2])) then
local Tool = Player.Character:FindFirstChildOfClass("Tool")
local Human = Player.Character:FindFirstChildOfClass("Humanoid")
if Tool and Human then
Human:UnequipTools()
wait()
Tool.Parent = Target.Backpack
end
end
wait()
end
end,
}
game.Players.PlayerAdded:Connect(function(Player)
if Player:GetRankInGroup(Settings.Group) >= Settings.Rank then
Player.Chatted:Connect(function(Message)
if string.sub(Message,1,1) == Settings.Prefix then
local Command = GetWords(string.sub(Message,2),"[%w_]+")
if ChatFunctions[Command[1]] then
ChatFunctions[Command[1]](Command,Player)
end
end
end)
end
end)
It is having an error, it seems to me that the top of the script wasn’t pasted in correctly, with the table identifying the prefix, group ID, and rank ID.
Oops seem like I had a small accident pasting it here.
Can you try this one ( I added the first line )
local Settings = {
Prefix = ":",
Group = 8676742,
Rank = 4,
}
function GetWords(Msg,Pattern)
local Words = {}
for w in string.gmatch(Msg, Pattern) do
table.insert(Words,w)
end
return Words
end
local ChatFunctions = {
["handto"] = function(Words,Player)
for _,Target in pairs(game.Players:GetPlayers()) do
if string.find(string.lower(Target.Name),string.lower(Words[2])) then
local Tool = Player.Character:FindFirstChildOfClass("Tool")
local Human = Player.Character:FindFirstChildOfClass("Humanoid")
if Tool and Human then
Human:UnequipTools()
wait()
Tool.Parent = Target.Backpack
end
end
wait()
end
end,
}
game.Players.PlayerAdded:Connect(function(Player)
if Player:GetRankInGroup(Settings.Group) >= Settings.Rank then
Player.Chatted:Connect(function(Message)
if string.sub(Message,1,1) == Settings.Prefix then
local Command = GetWords(string.sub(Message,2),"[%w_]+")
if ChatFunctions[Command[1]] then
ChatFunctions[Command[1]](Command,Player)
end
end
end)
end
end)