Text Won't Change

I"m trying to set this script up to kill the player if the text is “Two”. Right now the issue is that the text isn’t changing.

local textLabel = script.Parent.SurfaceGui.TextLabel
local part = script.Parent
local messages = {"One", "Two", "Three"} 
local index = 1

while true do
	textLabel.Text = messages[index]
	if index == 2 then
		part.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				hit.Parent.Humanoid.Health = 0
			end
		end)
	else
		part.Touched:Disconnect()
	end
	index = index + 1
	if index > #messages then
		index = 1
	end
	wait(5)
end

IF I remove this part the text changes but obviously it wont kill the player, as i’m trying to do.

if index == 2 then
		part.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				hit.Parent.Humanoid.Health = 0
			end
		end)
	else

Have you tried instead of using that part fire a RemoteEvent and form another script kill the player? I know it takes a lot of time but it might be the solution

1 Like

There are many things you can do to improve this code, I’ll go over some:

For your Connection, you should FIRST create a variable to hold this connection, this is so you wont lose the connection, what you are doing with part.Touched:Disconnect() WILL NOT WORK, as that is not how :Connect(), works, :Connect() returns the signal, so It would useful to store it (like I said)

local connection = part.Touched:Connect(function(hit) -- returns Connection
end)

if Condition and connection then -- if the required condition is met and there is a connection
connection:Disconnect() -- disconnects connection
connection = nil -- Removes Connection
end

As for your index Variable, instead of you using an if statement for this, you can use the Modulo (%) Operator to do this job for you, If you don’t know, Modulo returns the remainder in Division, if you have 1 % 2, it will return 1, but if you have 2 % 2, it will return 0, this is useful as when our number reaches this set limit, it will automatically be reverted back to 0, but, we want it to be one!, so we would use math.max() to get the largest number for when that happens, so with this knowledge, we can now do this:

index = math.max(1, index % #messages + 1)

Instead of

if index > #messages then
	index = 1
end

Which after testing, I can confirm works:
Output:

  19:52:25.132  > print(math.max(1, 0 % (3 + 1)))  -  Studio
  19:52:25.133  1  -  Edit
  19:52:25.542  > print(math.max(1, 1 % (3 + 1)))  -  Studio
  19:52:25.543  1  -  Edit
  19:52:27.964  > print(math.max(1, 2 % (3 + 1)))  -  Studio
  19:52:27.964  2  -  Edit
  19:52:31.960  > print(math.max(1, 3 % (3 + 1)))  -  Studio
  19:52:31.961  3  -  Edit
  19:52:34.014  > print(math.max(1, 4 % (3 + 1)))  -  Studio
  19:52:34.014  1  -  Edit

For the Explaination:
We are simply returning the remainder of this division, since we are trying to the number of the items with the table, we add one, this is because if we do it normally with 3 % 3, it will skip 3 and go back to 0, so we add + 1 to the number of items in the table to ensure this doesn’t happen, so 3 + 1 is 4 and now if we test the code: 3 % 4, it wouldn’t skip as it is now looking for the value 4 instead of 3, math.max() is used to get the Largets number within its arguments, so it would be beneficial for use to be using, we can make it so if the Remainder is 0 and not 1, we can automatically set it to 1 by using math.max():

  local index = 0
  index = math.max(index, 1)
  -- Basically what the Code returns:
  19:59:58.688  > print(math.max(0, 1))  -  Studio
  19:59:58.688  1  -  Edit

Hope that helps with the understanding.


As for your Text, If its Instead a LocalScript, make sure that it isn’t in the workspace unless you have Change a Server Scripts RunContext to Client, otherwise normal LocalScripts cannot work in that environment. only on Players ReplicatedFirst, StarterGui, PlayerGui, StarterPlayerScripts, StarterCharacterScripts

you should Instead Adornne the SurfaceGui from StarterGui to the Expected Part so you can use Regular LocalScripts


Another thing to note is that wait() is deprecated and should be replaced with task.wait(), task.wait() is a lot faster than wait() being more accurate and much more efficient for performance.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.