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

Player Misinterpreted for a part when it's being told to another script?

Hi Everyone!

I was making this code for my game of tag, which attempts to detect a person in-game who reset, to respawn them back into the map. I use this code below to fire a message to the client:

for i = 1, #playersinround do
		player = playersinround[i]
		print(player) -- prints "Git_Good445"
		print(playersinround[1]) -- prints "Git_Good445"
		game.ReplicatedStorage.MapSpawns:FireClient(player, MapSpawns, playersinround)
	end

(playersinround is a table containing the players of the people who are in the round, and its important to note that the containments of the MapSpawns table are regular parts)

Then, i use this code below to fire a message to a different script than the one before.

game.ReplicatedStorage.MapSpawns.OnClientEvent:Connect(function(MapSpawns, playersinround)
	print(playersinround[1]) -- prints "Git_Good445"
	game.ReplicatedStorage.MapSpawns:FireServer(MapSpawns, playersinround)
end)

Finnaly, the message goes to this code in a different server script, that the 1st one.

CurrentStatus = game.ReplicatedStorage.CurrentStatus

game.ReplicatedStorage.MapSpawns.OnServerEvent:Connect(function(mapspawns, PIR)
	MapSpawns = mapspawns
	playersinround = PIR
	print(playersinround[1].Name) -- prints "Part"
end)


game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			print(plr.Name .. " has died!") -- prints
			plr.CharacterAdded:Connect(function()
				print(plr.Name .. " has respawned") -- prints
				if CurrentStatus.Value == "Round" then
					print("Round In Progress") -- prints
					print(playersinround[1].Name) -- prints "Part"
					print(#playersinround) -- prints "9", another problem
					for i = 1, #playersinround do
						print("Is " .. plr.Name .. " in the round?")
						local playerName = playersinround[i].Name
						print("Person in round is " .. playerName) -- prints "Person in round is Part"
						if playerName == character.Name then
							print(plr.Name .. " is in the round") -- doesnt print
							if character:FindFirstChild("HumanoidRootPart") then
								print("HumanoidRootPart Found") -- doesnt print
								local spawnNumber = math.random(1, #MapSpawns)
								character.HumanoidRootPart.CFrame = MapSpawns[spawnNumber].CFrame + Vector3.new(0,7,0)
							end
						end
					end
				end
			end)
		end)
	end)
end)

For some reason, when it gets to the other server script, the player is misinterpreted for a regular part. Why is it doing that?

Any help is appreciated :)

1 answer

✓ Accepted answer

This is because you forgot to account for the "Client who fired" parameter imposed through the OnServerEvent signal. Therefore, each parameter enlisted will not contain the proper entities that were fired in their respective order. Each variable will actually represent:

mapspawns = Player Object
playerinround = mapspawns

As you said, mapspawns is an array holding a list of BasePart Instances. Calling playersinround[1] will be translating to

print(mapspawns[1]) --> Part

To fix this, simply adjust your parameters:

RemoteEvent.OnServerEvent:Connect(function(_, mapspawns, playersinround)
--// Underscore identifies we wish to ignore that parameter.

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