Conversion Error - trying to increase text value +1

Hello, I am having a problem when trying to increase the value of the text +1, the error is the following:

  10:09:36.218  Counter value cannot be converted to a number.  -  Servidor - Script:21

and below I share my script, how could I improve this so that it works? I’m not sure how to do the conversion… :frowning:

script code:

-- Get the ProximityPrompt
local proxPrompt = script.Parent:WaitForChild("ProximityPrompt")

-- Connect the function to the ProximityPrompt's Triggered event
proxPrompt.Triggered:Connect(function(player)
	-- Print a message indicating that the teddy bear has been taken by the player
	print("Teddy bear has been taken by " .. player.Name)

	-- Access the counter TextLabel through StarterGui
	local counterTextLabel = game.StarterGui.gui_inGame.BackGraundGame.counter

	-- Check if the current counter text can be converted to a number
	local currentCounterValue = tonumber(counterTextLabel.Text)
	if currentCounterValue then
		-- Increment the counter value
		currentCounterValue = currentCounterValue + 1
		-- Update the counter text
		counterTextLabel.Text = tostring(currentCounterValue)
	else
		-- If conversion fails, print an error message
		warn("Counter value cannot be converted to a number.")
	end

	-- Wait for 2 seconds before destroying the teddy bear model
	wait(2)

	-- Destroy the teddy bear model
	script.Parent:Destroy()
end)
2 Likes

Can you show us an example of the counterTextLabel.Text, please? This is to make sure the value is able to be converted to a number without any problems

1 Like

Sure, here’s:

image

I see the problem, the text is 0/2 which isn’t able to be converted into a number because of the /

Edit: @PipeSader This might solve your problem:

-- Get the ProximityPrompt
local proxPrompt = script.Parent:WaitForChild("ProximityPrompt")

-- Connect the function to the ProximityPrompt's Triggered event
proxPrompt.Triggered:Connect(function(player)
	-- Print a message indicating that the teddy bear has been taken by the player
	print("Teddy bear has been taken by " .. player.Name)

	-- Access the counter TextLabel through StarterGui
	local counterTextLabel = game.StarterGui.gui_inGame.BackGraundGame.counter

	-- Splitting the counter's text to avoid problems with the /
	local text = string.split(counterTextLabel.Text, '/')

	-- Check if the current counter text can be converted to a number
	local currentCounterValue = tonumber(text[1])
	if currentCounterValue then
		-- Increment the counter value
		currentCounterValue = currentCounterValue + 1
		-- Update the counter text
		counterTextLabel.Text = tostring(currentCounterValue) .. '/' .. text[2]
	else
		-- If conversion fails, print an error message
		warn("Counter value cannot be converted to a number.")
	end

	-- Wait for 2 seconds before destroying the teddy bear model
	wait(2)

	-- Destroy the teddy bear model
	script.Parent:Destroy()
end)
1 Like

Thank you so much! It’s solved, thanks! :smiley:

1 Like

oh just got an error… sorry

12:07:15.209 Workspace.Teddy.Model.Script:36: attempt to concatenate string with nil - Servidor - Script:36

counterTextLabel.Text = tostring(currentCounterValue) .. '/' .. text[2]

On line 35 add this line:

print(currentCounterValue, text[1], text[2])

And then show me the output afterwards

1 Like

Thank you very much! It’s working now! I also realized that I should access the text correctly for it to update on the player like this:

	local counterTextLabel = player.PlayerGui:WaitForChild("gui_inGame"):WaitForChild("BackGraundGame"):WaitForChild("counter")
1 Like

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