How can I fix this script?

Hello,
I’m trying to create a script that checks the player character model and if it finds the model and the model has these attributes as in the photo below.

image

I want the Player´s health be same as value of Health attribute in model.
If there is also a ReflexUI attribute, I want to clone the Gui from the replicatedStorage and paste it the into player´s playerGui.

Code:

local Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Guis = ReplicatedStorage:WaitForChild(“Guis”)

local character = Players.LocalPlayer.Character
local player = Players:GetPlayerFromCharacter(character)
if not player then return end

local container = character.Parent:FindFirstChild(player.Name)
if not container then return end

for _, model in ipairs(container:GetChildren()) do
if model:IsA(“Model”) then
local healthValue = tonumber(model:GetAttribute(“Health”))
if healthValue then
local humanoid = character:FindFirstChildOfClass(“Humanoid”)
if humanoid then
humanoid.MaxHealth = healthValue
humanoid.Health = healthValue
end
end

	local reflexAttr = model:GetAttribute("ReflexUI")
	if reflexAttr == true then
		local guiTemplate = Guis:FindFirstChild("ReflexGUI")
		if guiTemplate then
			local guiClone = guiTemplate:Clone()
			guiClone:SetAttribute("TargetModel", model.Name)
			guiClone.Parent = player:WaitForChild("PlayerGui")
		end
	end
end

end

Any help would be appreciated.

1 Like

Hello, whats the problem exactly? (+ Can you paste the code in some Preformatted text please?, it hurts my eyes :sob:)

Hey brother, I’ll be frank, your code is horrible; however, if you are willing to learn, read and understand how lua works, we can get you on the right track.

Data Model link is meant to show you what you can access from a LocalScript, aka a Client-based script.
Data model | Documentation - Roblox Creator Hub

This link shows you exactly what a LocalScript is.
LocalScript | Documentation - Roblox Creator Hub

Some issues with your code, LocalScript can access the LocalPlayer directly, and you’ve already done it by creating the variable;

 local character = Players.LocalPlayer.Character
-- local Player = Player.LocalPlayer -- instead of Players:GetPlayerFromChar.....

Additionally, you can set the Clients Humanoid MaxHealth to whatever you want; however, IT WILL NOT be replicated to other clients. You must change the value on the Server to replicate Server-Client boundaries.


Your line below is calling the Workspace, and trying to find the Character again. You already have the character model from the variable local character.

local container = character.Parent:FindFirstChild(player.Name)

Your loop, “for _, model…” is a method of getting the Humanoid; however, you can easily utilize
Instance | Documentation - Roblox Creator Hub

:FindFirstChildOfClass("Humanoid")

There is no need to utilize an Attribute for the players health; however, for “ReflexUI” you should utilize the boolean attribute, instead of string.

I HIGHLY recommend you watch some youtube videos on “Roblox Server-Client replication” and “Lua for beginners”