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

Using a IntValue To subtract a Number?

Asked by 5 years ago
Edited 5 years ago

Theres a IntValue In Workspace Causing Me Creating a Value Of "ZombiesLeft" How Do I Make it Minus A Zombie After Health 0?

This Scripting Doesen't Seem To Work.

local message = Instance.new("Message")

while true do
rounds = Instance.new("IntValue")
rounds.Parent = game.Workspace
rounds.Name = "Rounds"
rounds.Value = 0

zombies = Instance.new("IntValue")
zombies.Parent = game.Workspace
zombies.Name = "ZombiesLeft"
zombies.Value = 0

next = Instance.new("IntValue")
next.Parent = game.Workspace
next.Name = "Next"
next.Value = 0

--Round 1 -- 2 Sets
message.Parent = game.Workspace
message.Text = "Round 1"
rounds.Value = 1
wait(1)
message:remove()
--Set 1
game.Workspace.ZombiesLeft.Value = game.Workspace.ZombiesLeft.Value+1
a = game.Lighting.ZombieA:clone()
a.Parent = game.Workspace
a:makeJoints()
wait(10)
--Set 2
game.Workspace.ZombiesLeft.Value = game.Workspace.ZombiesLeft.Value+1
a = game.Lighting.ZombieA:clone()
a.Parent = game.Workspace
a:makeJoints()
wait(10)
--Finishing Sets
next.Value = 1
--Ending Round 
if next.Value==1 then
next.Value = 0
message.Parent = game.Workspace
message.Text = "Round 2"
rounds.Value = 2
wait(1)
message:remove()
--Set 1
game.Workspace.ZombiesLeft.Value = game.Workspace.ZombiesLeft.Value+1
a = game.Lighting.ZombieA:clone()
a.Parent = game.Workspace
a:makeJoints()
wait(10)
--Set 2
game.Workspace.ZombiesLeft.Value = game.Workspace.ZombiesLeft.Value+1
a = game.Lighting.ZombieA:clone()
a.Parent = game.Workspace
a:makeJoints()
wait(10)
--Set 3
game.Workspace.ZombiesLeft.Value = game.Workspace.ZombiesLeft.Value+1
a = game.Lighting.ZombieA:clone()
a.Parent = game.Workspace
a:makeJoints()
wait(10)
--Finishing Sets
next.Value = 1
--Ending Round 
if next.Value==1 then
next.Value = 0
game.Workspace.Rounds:remove()
game.Workspace.ZombiesLeft:remove()
game.Workspace.Next:remove()
message.Parent = game.Workspace
message.Text = "You Survived!"
wait(2)
message:remove()
end
end
end

Other Part Inside MainGame Script

local hint = Instance.new("Hint")
hint.Parent = game.Workspace

while true do
hint.Text = "Round: "..game.Workspace.Rounds.Value.."/    Zombies: "..game.Workspace.ZombiesLeft.Value
wait(0.1)
end

0
to negitive? You mean subtract? hellmatic 1523 — 5y
0
You mispelled "ZombiesLeft" on the second one.You have it as "ZombieLeft" LuaDLL 253 — 5y
0
Also, not sure if this is your actual code but- you put 'game.Workspace.ZombieLeft' instead of 'game.Workspace.ZombiesLeft' on the right side of the = sign. zaniac10 15 — 5y
0
wrap your code in Humanoid.Died event hellmatic 1523 — 5y
View all comments (4 more)
0
i Dont UnderStand The Humanoid.Died event PreloadContent -1 — 5y
0
Look at my answer PreloadContent LuaDLL 253 — 5y
0
Its basically when the humanoid's health reaches 0. I think it also fires when you use the :BreakJoints() function theking48989987 2147 — 5y
0
Use :Destroy(). LoganboyInCO 150 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

Hey, I hope we can get this script working for you. First thing I notice is that you are using Lighting for storage. That is not recommended and there are already great alternatives in place for you. You can use ReplicatedStorage or ServerStorage. Note that things you place into ServerStorage will not be replicated to the client. This means that you can save a lot of lag by keeping maps and things in there.


The next thing I notice you using is :remove() which is deprecated. If you read the developer page, you will see that it says:

"This item is deprecated in favor of Instance:Destroy and Instance:ClearAllChildren. If you must >remove an object from the game, and wish to use the object later, set its Parent property to nil >instead of using this method."

I recommend that you use :Destroy() instead.


The next thing I notice is your lack of proper indentation. This leads to unclear and hard to read code. Please look into writing clean and good code. It not only increases readability, but it also helps spot errors before they even become an issue.


A quick note about :Clone(), is that the c is capitalized and the lowercase c version of :Clone() is deprecated. Please keep up to date on the proper methods for scripting in the Roblox environment.


Both the instance Hint and the instance Message have been deprecated with the introduction of Roblox's GUI features. I recommend you try to figure out a different way to display the round to the player.


I do recommend that when getting a service, you use :GetService(). A great explanation as to why can be found here. Note: game.Workspace is kind of an exception to this rule.


I see that you are using next as a variable name. Because next is a Lua method, I don't recommend that you use that particular name. It is an iterator method similar to ipairs and pairs. More info on that here.


A small but important thing is that you should really be setting the parent property after setting all the other properties. Here is an example taken from your script:

rounds = Instance.new("IntValue") 
rounds.Name = "Rounds"
rounds.Value = 0
rounds.Parent = game.Workspace -- setting the parent last
-- it is just better for performance

Ok, after covering all of the issues in your script, let us get down to business. The first thing I see is that you are using a while loop to change the text of the hint. This might work fine, but it is inefficient when Roblox has provided us with the Changed event. Here is an example of how it can be used in your script, replacing the while loop:

--[[ 
    assuming hint has already been defined
    while I will still use hint in this example,
    I highly recommend that you change 
    the way you show the round and zombie count
--]]
local round = game.Workspace.Rounds -- use variables to your greatest advantage
local zombiesLeft = game.Workspace.ZombiesLeft
zombiesLeft.Changed:Connect(function()
    hint.Text = "Round: "..rounds.Value.."/    Zombies: "..zombiesLeft.Value
end)
-- I will do a changed event for both rounds and zombies so the player's screens are always displaying the correct data
round.Changed:Connect(function()
    hint.Text = "Round: "..rounds.Value.."/    Zombies: "..zombiesLeft.Value
end)
-- I will provide some links at the end of the page where you can read more on how things work

Note: I do recommend that you use ReplicatedStorage for the round and zombie amount values because Workspace is usually for 3D objects in the world. ReplicatedStorage is also a great option because the client can access it, which is helpful if you switch over to using GUIs for your display.


Finally, we get to the value of ZombiesLeft changing. I recommend that you use the Died event to change the value. The easiest way to do this would be to connect the Died event to the zombie when you place him into the world. I will provide you with an example from your script:

-- remember to use variables to help yourself write less
local zombiesLeft = game.Workspace.ZombiesLeft
local serverStorage = game:GetService("ServerStorage") -- using the recommended method for getting a service
zombiesLeft.Value = zombiesLeft.Value + 1
a = serverStorage.ZombieA:Clone -- pretending that ZombieA is not in Lighting because storing things in Lighting is bad practice
a:makeJoints()
local humanoid = a:FindFirstChild("Humanoid") -- getting the humanoid to which we will attach the Died event
if humanoid then -- just want to make sure it is there
    humanoid.Died:Connect(function()
        zombiesLeft.Value = zombiesLeft.Value - 1
        -- you can include other effects in here, but that is up to you
    end
end
a.Parent = game.Workspace
-- quick note: Instead of using one letter variables, try to use variable names that make sense and point to what they are holding

I hope this helps and have a great day scripting!


0
This is only to point you in the right direction. I am not going to write your code for you. User#21908 42 — 5y
0
Omg you went in HilyrHere 79 — 5y
Ad
Log in to vote
0
Answered by
LuaDLL 253 Moderation Voter
5 years ago
Edited 5 years ago

Your two solutions can be:

game.Workspace.ZombiesLeft.Value = game.Workspace.ZombiesLeft.Value-1

or

Put a script inside the zombies

local Zombie = script.Parent
local Humanoid = Zombie:FindFirstChild("Humanoid")
local ZombiesLeft = game.Workspace.ZombiesLeft

Humanoid.Died:Connect(function()
    ZombiesLeft.Value = ZombiesLeft.Value - 1
end

Since you dont understand the Humanoid.Died function ill explain it simply.

Humanoid.Died is a function that is called whenever a Humanoid has died (Or reached 0 Health)

0
Assuming you didnt have it in a Died function already. LuaDLL 253 — 5y
2
Died is an event, which is fired, not called. User#19524 175 — 5y

Answer this question