Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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!

0
LocalScripts have no access to ServerStorage's descendants. Try moving GreyHair to ReplicatedStorage. DeceptiveCaster 3761 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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.

0
HealthChanged exists for a reason? DeceptiveCaster 3761 — 4y
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? Primrose_Studio 53 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Answer this question