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

A tricky question... about changing an appearance after its already set?

Okay, so I have an idea... In my game when I player kills another player I want the killer's appearance to switch to the person they killed. How would I do since, this function seems to be only abled to be called when the player joins?

local appearance = math.random(16, 145599999)

game.Players.PlayerAdded:connect(function(Player)
    Player.CharacterAppearance = "http://assetgame.roblox.com/Asset/CharacterFetch.ashx?userId="..appearance.."&placeId=YOURPLACEID"
end)

and yes, I know the function will be called when the player joins or respawns, but that's just it, the function will not work in say this form...


function thiswontwork ()
local appearance = math.random(16, 145599999)
for _, player in pairs(players) do
    player.CharacterAppearance = "http://assetgame.roblox.com/Asset/CharacterFetch.ashx?userId="..appearance.."&placeId=453266817"
end
end

The above will not work, even this wont work:


game.Players.PlayerAdded:connect(function(Player)
local appearance = math.random(16, 145599999)
wait(0.1)
    Player.CharacterAppearance = "http://assetgame.roblox.com/Asset/CharacterFetch.ashx?userId="..appearance.."&placeId=YOURPLACEID"
end)

It seems to me that once the appearance id is set, you can't change it. So how can I change it even when it is set?

The admin houses allow for this, where you can use an admin type command to change to the player appearance id of your choice.

Thanks in advance

1 answer

It was not working because you did not properly define 'players' in your code.

Here is your code:

function thiswontwork ()
	
local appearance = math.random(16, 145599999)
for _, player in pairs(players) do
    player.CharacterAppearance = "http://assetgame.roblox.com/Asset/CharacterFetch.ashx?userId="..appearance.."&placeId=453266817"
end

end


Simply, you did not define the location of players correctly.

You don't do

in ipairs(players) do

You do this instead:

in ipairs(game.Players:GetChildren()) do

Here is the finished code, done correctly for each possible way. < : - )

Function:

function cai()
	
local appearance = math.random(16, 145599999)

	for _, player in pairs(game.Players:GetChildren()) do
	    player.CharacterAppearance = "http://assetgame.roblox.com/Asset/CharacterFetch.ashx?userId="..appearance.."&placeId=462340102"
	end

end
wait(1.5) -- keep it at 1.5 for safe measures.
cai()

Player Added:

game.Players.PlayerAdded:connect(function(Player)
	local appearance = math.random(16, 145599999)
	print(appearance)
	
	wait(0.3) -- keep it at 0.3 for safe measures.
     
	Player.CharacterAppearance = "http://assetgame.roblox.com/Asset/CharacterFetch.ashx?userId="..appearance.."&placeId=462340102"
end)

The one I did not include wouldn't really give you a random appearance each time, it would then be the same for all players. (because it is only defined once instead of redefining each time someone joins)

I tested both of this scripts that I wrote and they worked in studio. I am pretty sure they'll work in-game as well.

2 comments

0

mmmm, well you see here, I did define players, just not here lol the function didnt seem to work for me, is it local or global, what script type does it belong? Either way it doesn't work, but gives no errrors

0

Give me your entire script, then I may be able to make it work for you, otherwise everything I told you does work, its just the way you use it, makes it break. (I did a full test on both as I did mention)

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