Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

string.gsub() Changing a number value on Touch?

Hey there, New scripter here,

I'm trying to create a Quest like system were there is a GUI at the top of the screen saying "Books Found 0/4"

Now im trying to create it were when you touch a Part(Book) the number would change to 1/4 ect ect...

Not sure were to really start but i understand string.gsub seems right?

Thanks,

2 comments

0

This is a duplicated topic. Please delete this or the other one.

0

I was unsure how to implement what you told me last time, Im super new and have 0 clue about string.gsub

2 answers

✓ Accepted answer

Here are the steps:

  1. First, add a RemoteEvent into ReplicatedStorage and name it to bookCollected
  2. Second, add a LocalScript into the text you are displaying this in
  3. Third, add a ServerScript into the books

Here's the code:

ServerScript inside the book Object

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		game.ReplicatedStorage.bookCollected:FireClient(player)
	end
end)

LocalScript:

local text = script.Parent
local booksFound = 0 -- by default unless you save it, I'll let you deal with that since it isn't related to the question

game.ReplicatedStorage.bookCollected.OnClientEvent:Connect(function()
	booksFound = booksFound + 1
	
	if booksFound < 4 then
		text.Text = 'Books Found ' .. booksFound .. '/4'
	else
		text.Text = 'Books Found 4/4'

		-- Add additional code to do when quest is complete
	end
end)

That should do the job!

10 comments

0

Were do i insert this into?, and will it update OnTouch with a Part?

0

I'll update the answer in a second...

0

Thanks worked nicely, However it can go past 4/4 would there be a way to stop it?, Maybe debounce from what i understand?

0

oh let me fix that

0

Works nicely and added a debounce!, Thanks alot

0

welcome :)

0

Hey, I just noticed something when testing that its Local i kinda need it to be seen by everyone when touched?, Is that a quick fix?

0

what seen?

0

The Books Found numbers going up, As of now if Player1 Finds a book it will only say 1/4 Books found on his screen i need it to be Server side,

0

is it a like a group quest?

Like I stated before, if your text label is a fixed “0/4 Quests” then you should use ‘string.gsub’ There are patterns and the one your looking for is ‘%d’ which finds the first number in a text/string.

Here’s an example:

local textlabel = script.Parent — this label will have an example text like “0/5 Found”

if player_found_quest then
    local number = tonumber(textlabel.Text:match(‘%d’)) — this will give you ‘0’ since it is the first digit of that text
    textlabel.Text = string.gsub(textlabel.Text, ‘%d’, tostring(number+1))
end

— this will add 1 to the text for example: “1/5 Found”

2 comments

0

I understand how it would work, But i can't see as of now how i would make it work, Im also getting Expected identifier

1

I’m sorry for the last few days my answers were written on mobile, code is not guaranteed to fit within the syntax, however you can simply get it to work by using string.gsub() and like in that “if” statement you just need to insert that code and it’ll be automatic. If it fails do some changes.

This discussion is preserved as it was. New answers, comments, and votes are closed.