GENERAL PRACTICE
:children() is deprecated in favor of :GetChildren(), However, to get a table of players, it is better to use the :GetPlayers() function of the Players Service since if an object was added to the players service with the same name as a player, you may grab it by mistake.
Instead of using for i = 1, #table and the players[i], you can use for index, value in pairs(table) do instead as it is more condensed and efficient
":findFirstChild()" should be capitalized to :FindFirstChild()
Use :WaitForChild() to make sure that a part exists before using or changing it when you know it is supposed to be there.
You can use :GetPlayerFromCharacter() to get the player of a character model more directly within a touched event than iterating over a table.
To prevent spam of the door, use a debounce as a cooldown for the door to complete its changes.
ISSUES
You can place a function's connection in the same line as its definition. (You also wrote connect =, which is not required)
In order to save data, you must use a DataStore which will save values such as numbers and strings. Generally, we add a pcall to this to make sure that errors are circumvented and do not stop the rest of the script from running. DataStores are generally used to load data when a player joins the game, and then saves the data once they leave. As a result, if we change the data value we want to save on the server, when the player leaves, this change will stay in place. Below, the script will check that the bool is true, if it is false, then it will change the value to true and remove 250 points from the player, if it is true, then the value will remain the same and no points will be deducted.
REVISED SERVER SCRIPT
local DSS = game:GetService("DataStoreService")
local Storage = DSS:GetDataStore("Score")
local Door = script.Parent
local debounce = true
game:GetService("Players").PlayerAdded:Connect(function(player)
local bool = Instance.new("BoolValue")
bool.Name = "RemoveDoor1"
bool.Value = false
bool.Parent = player
local success, result = pcall(function()
Storage:GetAsync(player.UserId)
end)
if not success then
warn(result)
else
local LoadValue = Storage:GetAsync(player.UserId)
if LoadValue then
bool.Value = LoadValue
print(bool.Value)
end
end
end)
game:GetService("Players").PlayerRemoving:Connect(function(player)
Storage:SetAsync(player.UserId, player:WaitForChild("RemoveDoor1").Value)
end)
Door.Touched:Connect(function(hit)
if debounce == false then return end
debounce = false
print("Door Hit")
local human = hit.Parent:FindFirstChild("Humanoid")
if human ~= nil then
print("Human touched door")
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player ~= nil then
local sp = player:WaitForChild("leaderstats"):WaitForChild("points")
local check = player:WaitForChild("RemoveDoor1")
if check.Value == false and sp.Value >= 250 then
print("Human passed test")
check.Value = true
sp.Value = sp.Value - 250
Door.CanCollide = false
wait(0.129)
Door.CanCollide = true
elseif check.Value == true then
print("Human already has access")
Door.CanCollide = false
wait(0.129)
Door.CanCollide = true
end
end
end
debounce = true
end)
3 comments
i did
It didn't load correctly, so I edited it, btw you talk about saving the door setting but I dont see a DataStore in this code...?
i dont know how to make it save is the problem