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…
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)
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
-- 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)