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

How can I make a script that makes an avatar's appearance change depending on health?

I've tried this script here:

wait(1)
local players = game:GetService("Players")
local player = game.Players.LocalPlayer
game:GetService("ServerStorage")

while true do
	if player.Character:FindFirstChild("Humanoid").Health==75 then
		game.Players.LocalPlayer.Character.Humanoid:AddAccessory(game.ServerStorage.GreyHair)
		wait(5)
	else 
		print("nope")
		game.Players.LocalPlayer.Character.Humanoid:AddAccessory(game.ServerStorage.GreyHair)
		wait(5)
	end
end

But I get errors saying that 'GreyHair' is not a valid member of ServerStorage. Is there a more efficient way of doing this? Note: I would like to just change the hair and face of the character. Thank you!

1 comment

0

LocalScripts have no access to ServerStorage's descendants. Try moving GreyHair to ReplicatedStorage.

2 answers

The error: First things first, the error is caused because ServerStorage is not accessible from LocalScripts; it can only be reached by the server. Instead, put your hair in ReplicatedStorage, as this is visible to both the server and the client (hence replicated).

That while loop: With that error fixed, your code will run. However, there is a better way to keep check of the player's health than routinely running while true, as this isn't ideal for performance. A better option if GetPropertyChangedSignal. This is a function that will be called when the given property of an instance changes, i.e. it'll allow you to know exactly when the player's health changes:

local replicatedStorage = game:GetService("ReplicatedStorage")
local greyHair = replicatedStorage:WaitForChild("GreyHair")
local humanoid = player.Character.Humanoid

local function onHeathChange()
	if player.Character.Humanoid.Health == 75 then
		-- whatever it is you do, adding the grey hair or whatever
	else
		-- if it's not 75
	end
end

humanoid:GetPropertyChangedSignal("Health"):Connect(onHeathChange)

This script works by running the function onHealthChange whenever the player's health changes. This is much better than using while true do to check the player's health.

2 comments

0

HealthChanged exists for a reason?

0

Nope, still saying that Grey hair is not a valid member of ReplicatedStorage. The script is in StarterGui, in workspace it doesn't work, or anywhere else. Any more ideas?

or make this server sided so everyone else can see the change too

put the hair in server storage

server script

game.ReplicatedStorage.accessory.OnServerEvent:Connect(function(player,accessory)
if player.Character.Humanoid.Health == 75 then--makes it so it cant be exploited
player.Character.Humanoid:LoadAccessory(game.ServerStorage.GreyHair)
end
end)

local script

game.Players.LocalPlayer.Character.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
ïf game.Players.LocalPlayer.Character.Humanoid.Health == 75 then
game.ReplicatedStorage.accessory:FireServer()
end
end)

and ofcourse add a remote event in replicated storage called accessory unless u want to call it something else, if so change the script to that name

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