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

My script is unable to find a folder in workspace that is named the same as the player, any help?

Asked by 5 years ago
Edited 5 years ago

The client script runs, and it creates a folder, named exactly the player's name. When a car spawns, I am trying to find that folder, and check inside if there is an object, called 'Car'. If there is, then it will destroy it. The problem I am getting, is, that I am unable to find the folder in workspace through the className. Any help? (Both scripts are in ServerScriptService)

game.Players.PlayerAdded:Connect(function(plr)
    local cars = Instance.new("Folder")
        cars.Name = plr.Name 
        cars.Parent = workspace
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    local cash = Instance.new("IntValue")
    cash.Name = "Dollars"
    cash.Value = 122000
    cash.Parent = leaderstats

game.ReplicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player, NameOfCar)
    return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value
end
end)
--script runs when client starts

local Camaro = game:GetService("ReplicatedStorage"):WaitForChild("Camaro")
Camaro.OnServerEvent:connect(function(player)
    local car = game:GetService("ServerStorage").Cars.Camaro
    local veh = car:clone()
    local folder = workspace:FindFirstChild(player.className("Folder"))
        if folder:IsAncestorOf("Car") then
            folder.Car:Destroy()
        end
    veh.PrimaryPart = veh.Main
    veh.Parent = folder
    veh.Name = "Car"
    veh:SetPrimaryPartCFrame(player.Character.Head.CFrame)
end)
--Car spawn script

`

0
The Folder and IntValue instances should be created on the server script. xPolarium 1388 — 5y

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Lines 13-16 don't need to be inside the PlayerAdded() event. You should also verify that the car is there before tacking on ".Price.value". Also, make sure whatever you're creating is done via the server, not the client.

As far as lines 5-8, you're not going to be able to find the folder without a loop if you just search for their name, because it might find their character first. I would name the folder something more unique, like "Azarth car folder". You also need to a loop to find the Car.

local function findoldcarin(root)
    local map = root:GetDescendants()
    for i,v in pairs(map) do
        if v.Name == 'Car' then
            return v
        end
    end
    return false
end

local foldername = string.format("%s car folder", player.Name)
-- You can use the this to rename the folder in your first script as well. 

local folder = workspace:findFirstChild(foldername)
local car = folder and folder.ClassName == 'Folder' and findoldcarin(folder)
if car then 
    car:Destroy()
    print("Car destroyed")
else
    -- check out ternary operations for shorthand if statements 
    print( folder and 'found folder' or 'no folder')
    print ( not folder and 'no folder no car' or car and 'found car' or 'no car')
end
0
Thanks a lot, I appreciate it! yourbestfriend356 19 — 5y
Ad

Answer this question