Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by
Aozwel 71
4 years ago

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,

0
This is a duplicated topic. Please delete this or the other one. SmartNode 383 — 4y
0
I was unsure how to implement what you told me last time, Im super new and have 0 clue about string.gsub Aozwel 71 — 4y

2 answers

Log in to vote
1
Answered by
U_srname 152
4 years ago
Edited 4 years ago

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!

0
Were do i insert this into?, and will it update OnTouch with a Part? Aozwel 71 — 4y
0
I'll update the answer in a second... U_srname 152 — 4y
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? Aozwel 71 — 4y
0
oh let me fix that U_srname 152 — 4y
View all comments (6 more)
0
Works nicely and added a debounce!, Thanks alot Aozwel 71 — 4y
0
welcome :) U_srname 152 — 4y
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? Aozwel 71 — 4y
0
what seen? U_srname 152 — 4y
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, Aozwel 71 — 4y
0
is it a like a group quest? U_srname 152 — 4y
Ad
Log in to vote
1
Answered by
SmartNode 383 Moderation Voter
4 years ago

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”
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 Aozwel 71 — 4y
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. SmartNode 383 — 4y

Answer this question