So i started this but it isn't counting so it can't change the Text?
local state = script.Parent.State local math = 1 if math == 1 then state.Text = "Responding" end if math == 2 then state.Text = "OnStation" end if math == 3 then state.Text = "OnScene" end if math == 4 then math = 1 end if script.Parent.Up.MouseButton1Click:connect() then math = (math+1) print(math) end if script.Parent.Down.MouseButton1Click:connect() then math = (math-1) end
Could anyone help with counting i don't know what i do bad? My error is : attemt to call a nil value
Hey there, a few things with this script:
You're putting a Statement as a Event which doesn't work. You could: 1. Make it a normal event or 2. something.Event:Wait() which will wait until the event happens. (More explanation on wiki)
When scripting notice how the word "math" changes colors, I believe that is enough of a signal to tell you not to write that as a Variable name, a better name would be something like
count
Ok, so how do you fix this script? First we write all the variables that may be used:
local plr = game.Player.LocalPlayer -- this may be useless now but may be useful later on. local state = script.Parent.State local count = 1
Ok, now what we want to do is make a function, this function will update the text. This may seem a bit advanced but it's basically a variable for a set of command. Let's called our function UpdateState()
local plr = game.Player.LocalPlayer -- this may be useless now but may be useful later on. local state = script.Parent.State local count = 1 local function UpdateState() end
Now we want to input your if statements in. We also want to add elseif's
these bad boys will help reduce your lines.
local plr = game.Player.LocalPlayer -- this may be useless now but may be useful later on. local state = script.Parent.State local count = 1 local function UpdateState() if count == 1 then state.Text = "Responding" elseif count == 2 then state.Text = 'OnStation' -- you can also quote with ' elseif count == 3 then state.Text = 'OnScene' elseif count == 4 then count = 1 end end
Now let's do the following:
- Call the function so it runs
- Fix your events
local plr = game.Player.LocalPlayer -- this may be useless now but may be useful later on. local state = script.Parent.State local count = 1 local function UpdateState() if count == 1 then state.Text = "Responding" elseif count == 2 then state.Text = 'OnStation' -- you can also quote with ' elseif count == 3 then state.Text = 'OnScene' elseif count == 4 then count = 1 end end UpdateState() script.Parent.Up.MouseButton1Down:Connect(function() -- avoid :connect and use :Connect count = count + 1 print(count) UpdateState() -- update end) script.Parent.Down.MouseButton1Down:Connect(function() count = count - 1 print(count) UpdateState() end)
A few notes:
- this script will WORK to a degree
- You will have an issue of people spamming the buttons
- you will have an issue of where count goes into the negatives
Now I will let you solve these on your own.
Hopefully, this helped.
Best of luck developer!
if
statement, which is bound to throw you an error. Instead, create it like this:-- LocalScript local Math = 1 -- don’t call it math with lowercase m, that’s already a global variable. script.Parent.Up.MouseButton1Click:Connect(function() Math = Math + 1 print(Math) end) script.Parent.Down.MouseButton1Click:Connect(function() Math = Math - 1 print(Math) end)
If it's click then use MouseButton1Click, otherwise use MouseEnter. Full tutorial on Roblox Wiki