I want to make player health more than 100 when they having different ability
for example:
when player have stand called SP
the maxHealth goes to 350
when player have stand called Tw
MaxHealth goes to 375
local Player = game:GetService("Players").LocalPlayer
game.Players.PlayerAdded:Connect(function(Player)
local Character = Player.Character
local StandV = Character:FindFirstChild("Value"):WaitForChild("StandValue")
local H = Character:WaitForChild("Humanoid")
if H then
if StandV.Value == "SP" then
wait(2)
H.MaxHealth = 350
end
end
end)
local char = script.Parent
local hum = char:WaitForChild'Humanoid'
local StandV = Character:FindFirstChild("Value"):WaitForChild("StandValue")
if StandV.Value == "SP" then
wait(2)
hum.MaxHealth = 350
hum.Health = hum.MaxHealth
end
I see you’re using “game.Players.LocalPlayer” which is only defined in local scripts, this means that the change to MaxHealth will only reflect locally to the client (as opposed to it reflecting to the entire server for each client).
Use the following in a server script in the ServerScriptService folder:
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
if character:FindFirstChild("Value") then
local val = character:FindFirstChild("Value")
local standV = val:WaitForChild("StandValue")
if standV.Value == "SP" then
task.wait(1)
humanoid.MaxHealth = 350
humanoid.Health = 350
end
end
end)
end)
Why are you storing stats inside the player anyway? Store them inside the player instead either in leaderstats or any generic folder.
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
if player:FindFirstChild("Value") then
local val = player:FindFirstChild("Value")
local standV = val:WaitForChild("StandValue")
if standV.Value == "SP" then
task.wait(1)
humanoid.MaxHealth = 350
humanoid.Health = 350
end
end
end)
end)
Use this and parent the stats to the player instance instead, this will work since I’ve tested both scripts myself but you shouldn’t store stats inside the character.
I agree with Limited_Unique, store the stats outside the character because the character gets refreshed every time they die.
However if you really want to do it this way, you would need to check if the value exists inside the character, and then check if it has changed its property. This can be done like so:
-- make sure this is server side script:
local Players = game:GetService("Players")
local function updateHealth(character)
local humanoid = character:WaitForChild("Humanoid")
local SV = character:WaitForChild("Value"):WaitForChild("StandValue")
local function checkSV()
if SV.Value == "SP" then
humanoid.MaxHealth = 350
humanoid.Health = humanoid.MaxHealth
end
end
checkSV()
SV.Changed:Connect(checkSV)
end
Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
updateHealth(character)
task.wait(1)
player.CharacterAdded:Connect(updateHealth)
end)
It’s probably a problem with your structure. Could you send a picture of the explorer and/or the code that creates+edits the “StandValue” StringValue?
The other possible problem is that there might be an error with your syntax. Does your code look like this:
local Players = game:GetService("Players")
local function updateHealth(character)
local humanoid = character:WaitForChild("Humanoid")
local SV = character:WaitForChild("Value"):WaitForChild("StandValue")
local function checkSV()
if SV.Value == "SP" then
humanoid.MaxHealth = 350
humanoid.Health = humanoid.MaxHealth
elseif SV.Value == "TW" then
humanoid.MaxHealth = 375
humanoid.Health = humanoid.MaxHealth
end
end
checkSV()
SV.Changed:Connect(checkSV)
end
Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
updateHealth(character)
task.wait(1) --Not needed btw. Doesn't break anything in almost all cases though.
player.CharacterAdded:Connect(updateHealth)
end)