You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? I’m Trying To Make Some Stats For My Game But All Just Randomly Broke
-
What is the issue? The Remote Event From Client Sends The Stats And Value To Server So The Server Can Update The Stats
-
What solutions have you tried so far? I’ve Tried Looking On Devforum But Didn’t Find Anything
The Server Script Should Update The Values And Another Local Script Should Display Them On Gui, But Only The Health Works Properly
- Script That Send Data To Server
local Players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local localPlayer = Players.LocalPlayer
local updateEvent = rs.Events:WaitForChild("UpdateValues")
local Humanoid = Players.LocalPlayer.Character:WaitForChild("Humanoid")
while task.wait(script:WaitForChild("HungerDelay").Value) do
warn("Hunger")
updateEvent:FireServer("Hunger", "PlayerStats", 1, "-")
end
- Script That Changes The Values
local UpdateValues = game.ReplicatedStorage.Events:WaitForChild("UpdateValues")
UpdateValues.OnServerEvent:Connect(function(plr, value, stat, amount, arith)
if value and stat and amount and arith then
local TrueStat = plr:FindFirstChild(stat)
if not TrueStat then
warn("Stat Not Found")
return
end
local TrueValue = TrueStat:FindFirstChild(value)
if not TrueValue then
warn("Value Not Found")
return
end
if arith == "+" then
TrueValue.Value = TrueValue.Value + amount
elseif arith == "-" then
TrueValue.Value = TrueValue.Value - amount
elseif arith == "*" then
TrueValue.Value = TrueValue.Value * amount
elseif arith == "/" then
TrueValue.Value = TrueValue.Value / amount
elseif arith == "toggle" then
if TrueValue:IsA("BoolValue") then
TrueValue.Value = not TrueValue.Value
else
warn("Invalid Arith for Value Type")
end
elseif arith == "set" then
if TrueValue:IsA("StringValue") then
TrueValue.Value = amount
else
warn("Invalid Arith for Value Type")
end
else
warn("Invalid Arith")
end
else
warn("Values Are Missing")
end
end)
- Script That Puts The Values On Gui
wait(1)
local Players = game.Players
local localPlayer = Players.LocalPlayer
local Hunger = localPlayer:WaitForChild("PlayerStats"):WaitForChild("Hunger")
local Thirst = localPlayer:WaitForChild("PlayerStats"):WaitForChild("Thirst")
local Energy = localPlayer:WaitForChild("PlayerStats"):WaitForChild("Energy")
local Radiation = localPlayer:WaitForChild("PlayerStats"):WaitForChild("Radiation")
local HealthBar = script.Parent.Health
local HNumber = HealthBar.Number
local HBar = HealthBar.Bar
local HungerBar = script.Parent.Hunger
local ThirstBar = script.Parent.Thirst
local EnergyBar = script.Parent.Energy
local RadiationBar = script.Parent.Radiation
local Humanoid = localPlayer.Character:FindFirstChildOfClass("Humanoid")
local function getValues(value, BarParent, Bar, Text)
local parentSize = BarParent.AbsoluteSize.x
local cappedValue = math.clamp(value.Value, 0, 100) -- Clamp the value between 0 and 100
-- Update the value in the stat itself
value.Value = cappedValue
Text.Text = cappedValue .. "%"
Bar:TweenSize(UDim2.new(0, (cappedValue / 100 * parentSize), 1, 0), 'Out', 'Linear', '0')
end
local function getHealth(Humanoid, BarParent, Bar, Text)
local parentSize = BarParent.AbsoluteSize.x
local cappedValue = math.min(Humanoid.Health, 100) -- Cap the value at 100
-- Update the value in the stat itself
Humanoid.Health = cappedValue
Text.Text = cappedValue .. "%"
Bar:TweenSize(UDim2.new(0, (cappedValue / 100 * parentSize), 1, 0), 'Out', 'Linear', '0')
end
getHealth(Humanoid, HealthBar, HBar, HNumber)
Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
getHealth(Humanoid, HealthBar, HBar, HNumber)
end)
getValues(Hunger, HungerBar, HungerBar.Bar, HungerBar.Number)
Hunger:GetPropertyChangedSignal("Value"):Connect(function()
getValues(Hunger, HungerBar, HungerBar.Bar, HungerBar.Number)
end)
getValues(Thirst, ThirstBar, ThirstBar.Bar, ThirstBar.Number)
Thirst:GetPropertyChangedSignal("Value"):Connect(function()
getValues(Thirst, ThirstBar, ThirstBar.Bar, ThirstBar.Number)
end)
getValues(Energy, EnergyBar, EnergyBar.Bar, EnergyBar.Number)
Energy:GetPropertyChangedSignal("Value"):Connect(function()
getValues(Energy, EnergyBar, EnergyBar.Bar, EnergyBar.Number)
end)
getValues(Radiation, RadiationBar, RadiationBar.Bar, RadiationBar.Number)
Radiation:GetPropertyChangedSignal("Value"):Connect(function()
getValues(Radiation, RadiationBar, RadiationBar.Bar, RadiationBar.Number)
end)
I Also Have Tools That Increase The Values
local Players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local Eat = rs.Sounds:WaitForChild("Eat")
local updateEvent = rs.Events:WaitForChild("UpdateValues")
local Tool = script.Parent
local Food = Tool:WaitForChild("Food")
local PlayAnim = Tool:WaitForChild("PlayAnim")
local isEating = false
Tool.Activated:Connect(function()
if isEating == false then
isEating = true
PlayAnim:FireServer("eat", "start")
Eat:Play()
wait(1.2)
updateEvent:FireServer("Hunger", "PlayerStats", Food.Value, "+")
PlayAnim:FireServer("eat", "end")
Tool:Destroy()
end
end)
Any Help Will Be Appreciated!