Ad
Log in to vote
0

How can I use a StringValue to search for specific items in a folder?

Asked by 13 days ago

I am working on a racing game, and I have a datastore which keeps track of which car a player has. It works, and I can save the player's car, but i'm not too sure how to load the player into the right car when a race starts. Here's how it works;

My cars are in a folder in ServerStorage called 'cars'. Each player has a StringValue inside of their player object containing the name of their car. The game is supposed to read that value and search for it inside of that folder of cars, clone it, and move it to workspace at a certain position.

I am just not sure how I would script the game to find the car in ServerStorage with that value.

Here is an example of what I am thinking of;

local cars = game.ServerStorage.Cars --the folder containing all the cars
local onRaceStart = game.ReplicatedStorage.onRaceStartEvent --remote event that marks the start of the race

game.Players.PlayerAdded:Connect(function(plr)
    local chosenCar = plr.DataStore.chosenCar.Value 
    --the string value with their car selection

    onRaceStart.OnServerEvent:Connect(function()
        local car = game.ServerStorage.Cars[chosenCar]:Clone().Parent=game.Workspace
        --finds correct car in ServerStorage and clones it to workspace
        car.DriveSeat:Sit(plr.Character.Humanoid) --puts the player in their car
    end)
end)

I know the code above to find the right car will not work but I'm not sure what to do. You get the idea right?

Thanks in advance!

Answer this question