Working while testing alone but in local test server it doesn't work?

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!

Hey,
Sorry to hear about your problem.

I have thought of some things you could do that may end up in the solution of your problem.

  1. 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]”)

1 Like

Thank you, will try it now and will update you.

1 Like

Alright I tested it and it still didn’t work.

Okay hold on I will re-write a hand-to script right now

Thank you! I really appreciate it.

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 )

Sure, group ID 8676742. Rank ID 4.

1 Like
	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)

There you go!

1 Like

This goes in a script in server script service I am guessing? And thank you so much!

Yeah and no problem!
(Tell me if anything goes wrong because I didn’t have much time to have tests on it but I am supossing it won’t have any errors)

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.

Wait can I take a look at the error?

image
Line two is this line:

Group = 8676742,

The “=” is what is underlined in red.

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)

Hmmm, nothing happens no errors when I say “:handto Player2”.

I think you must try publishing and trying it like that but I will try to find a solution to that shortly.

Alright, thank you. It means a lot.

Hey how is it going? Just checking sorry.