Hi, I have a game that if you're on a team, you have a GUI that lets you type someone's name. In the code is a LocalScript. The LocalScript contains:

script.Parent.FocusLost:Connect(function(enter)
	if enter then
		local player = script.Parent.Parent.Parent.Parent.Parent
		if script.Parent.Text == player.Name then
			game.ReplicatedStorage.Disguise:FireServer(player, true, 0)
		else
			local players = game.Players:GetPlayers()
			if #players > 0 then
				for i=1, #players do
					if players[i].Name == script.Parent.Text then
						disguise = players[i]
					end
				end
			end
			
			if disguise == nil then
				script.Parent.Text = ""
				script.Parent.Parent.Title.Text = "Player not Found"
				wait(3)
				script.Parent.Parent.Title.Text = "Disguise As..."
			else
				game.ReplicatedStorage.Disguise:FireServer(player, false, disguise.CharacterAppearanceId)
			end
		end
	end
end)

script.Parent.Text = script.Parent.Parent.Parent.Parent.Parent.Name

This works perfectly fine. It called the Disguise event and a Script is listening, responding with this code:

function refresh(player)
	if player.Character ~= nil then
		local rootpart = player.Character:WaitForChild("HumanoidRootPart")
		local returnpos = rootpart.CFrame
		player:LoadCharacter()
		repeat wait() until player.Character ~= nil
		local rootpart = player.Character:WaitForChild("HumanoidRootPart")
		rootpart.CFrame = returnpos
	end
end

local function onCreatePartFired(player, selfdisguise, id)
	if selfdisguise == true then
		player.CharacterAppearanceId = "http://www.roblox.com/Asset/CharacterFetch.ashx?userId="..player.UserId
	end
	if selfdisguise == false then
		player.CharacterAppearanceId = "http://www.roblox.com/Asset/CharacterFetch.ashx?userId="..id
	end
	refresh(player)
end
 
game.ReplicatedStorage.Disguise.OnServerEvent:Connect(onCreatePartFired)

And another piece of script in the character that will appropriately change your name depending on who you're disguised as (and also if you're even disguised at all):

v = game.Players:GetPlayerFromCharacter(script.Parent)
for a, mod in pairs(v.Character:children()) do
	if mod:findFirstChild("NameTag") then
		v.Character.Head.Transparency = 0 mod:Destroy()
	end
end

repeat wait() until v.Character ~= nil


local char = v.Character
local mod = Instance.new("Model", char)
if (string.sub(v.CharacterAppearanceId, 56) == 0 or string.sub(v.CharacterAppearanceId, 56) == v.UserId) then
	mod.Name = v.Name
else
	players = game.Players:GetPlayers()
	disguise = ""
	if #players > 0 then
		for i=1, #players do
			if players[i].UserId == string.sub(v.CharacterAppearanceId, 56) then
				disguise = players[i].Name
			end
		end
	end
	if disguise == "" then
		mod.Name = "(Player you disguised as has left the game. Reverting to normal name.)"
	else
		mod.Name = disguise
	end
end
local cl = char.Head:Clone()
cl.Parent = mod

local hum = Instance.new("Humanoid", mod)
hum.Name = "NameTag"
hum.MaxHealth = 0
hum.Health = 0

local weld = Instance.new("Weld", cl) weld.Part0 = cl weld.Part1 = char.Head
char.Head.Transparency = 1

if disguise == "" then
	wait(6)
	mod.Name = v.Name
end
wait(1)
script:Destroy()

However, when this script is ran, the player either has the:

mod.Name = "(Player you disguised as has left the game. Reverting to normal name.)"

line run on them even though they're not disguised when it does, and when disguising the player keeps their actual avatar, with the naming part working correctly. I believe this is an issue with Studio, however after testing in actual game servers I'm noticing the problem's glaring there. Does anyone know what's happening?

2 comments

1

The first parameter of OnServerEvent is automatically the player who fired the event, therefore you don't need to do :FireServer(player, true, 0) and instead may do :FireServer(true, 0)

0

Thanks @Vulkarin... This may sound weird but that actually fixes my script. Is there a way I can accept your answer?