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

How To Fix This Script To Update A String Value Automatically?

Asked by 1 year ago

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local stardestroy = ReplicatedStorage:WaitForChild("StarDestroyCondition")
local RunService = game:GetService("RunService")

game.Players.PlayerAdded:Connect(function(player)   
    while true do
        wait()
        local Levels = player:WaitForChild("leaderstats").Level.Value 
        local LevelString = script.Parent.Parent.StarterPlayer.StarterPlayerScripts.LocalArrowScript.Levels.Value               
        print("String Obtained", LevelString, "and Levels Obtained", Levels)
        if tonumber(Levels) == 5 then
        if Levels == LevelString then                               
            stardestroy:FireClient(player)                          
                print("Fired Successfully") 
            end
        end
    end
end)

Any idea on how I could brush up this script to be able to determine the amount of checkpoints automatically without me having to change the value each time??

Specifically this piece of coding, I want to change it in order for it to become automatic.

if tonumber(Levels) == 5 then

The thing is, this piece of coding is supposed to fire a remote event to the client side which will, in return, trigger this piece of coding on a local script:

StarDestroyer.OnClientEvent:Connect(function()  
            cloneStar:Destroy() 
            print("Destroyed")      
        end)

But in order for that to happen and work, I need the LevelString to contain the amount of checkpoints that my game possesses and if I add any more checkpoints, I would like it to be updated automatically.

2 answers

Log in to vote
0
Answered by 1 year ago

I've figured out how to automatically get all of my game's checkpoints into the LevelString String Value. Here's the code:

    coroutine.resume(coroutine.create(function()
        for i,Levels in ipairs(checkpoints:GetChildren()) do        
            if Levels:IsA("BasePart") and Levels ~= nil then            
                local NumLevels = tonumber(Levels.Name)
                if script.Parent.Parent.StarterPlayer.StarterPlayerScripts.LocalArrowScript.Levels.Value ~= NumLevels then  
                    script.Parent.Parent.StarterPlayer.StarterPlayerScripts.LocalArrowScript.Levels.Value = NumLevels   
                    print("Total Levels are", NumLevels)
                end
            end
        end
    end))

If you have any improvements or any feedback on this code, feel free to send them.

Ad
Log in to vote
0
Answered by
enes223 327 Moderation Voter
1 year ago

hey you! have you ever heard of enes? if you are in trouble, better call enes!

0
?? nathanielstanle 20 — 1y

Answer this question