How to subtract int value from server script

Thanks you a lot! and how could i get the stamina value?
How could I access that value and subtract it? (from a server script)

By example, stamina = stamina.Value - 15 how could i do that?

local stamina = Instance.new("IntValue")
stamina.Name = "Stamina"
stamina.Value = 100
2 Likes

Uh… you can do it like that…
I mean
Like this;
stamina.Value = stamina.Value - 15
Or
Stamina.Value -= 15

3 Likes

Unless I’m sorely missing something here, it should be as simple as
stamina.Value -= 15

3 Likes

I did that script from another different script (both are from server) and I need to take that int value and do a -15

I don’t get it.
You’re not parenting stamin to nothing so

Here is the working script that I tried myself:

local stamina = Instance.new("IntValue")

stamina.Value = 100

stamina.Value = stamina.Value - 15

-- this checks if it worked
if stamina.Value == 85 then
print("worked")
end
3 Likes

You’d need to either parent it somewhere you can find it, store it in a module script, or store it in _G.

Judging by the name, you’d want to parent the stamina to the player. You can do that with this:

stamina.Parent = player

You now need to get the player. You also probably want to create one for each new player. You can do that like this:

game:GetService("Players").PlayerAdded:Connect(function(player)
 local stamina = Instance.new("IntValue")
 stamina.Name = "Stamina"
 stamina.Value = 100
 stamina.Parent = player
end)

The function above runs each time a player is added to the game. You can read about this here (Handling Events).
Now when ever you want to change the stamina you can get the IntValue by finding the player. If you have the character you can get the player like this:

local character --Assume this equals the character model (rig)
local player = game:GetService("Players")
local stamina = player.Stamina --Note you might want to either check if stamina exists first to avoid an error or use :WaitForChild if you really need it (and expect it to be there).

If your stamina value is inside the player or within a folder named “leaderstats” or something, you need a type a code in server script that looks like this:

game.Players.PlayerAdded:Connect(function(player)
       -- access the stamina inside player
end)

Thats easy, just do

stamina.Value = stamina.Value - 15