I am trying to make it so that when someone cheats it teleports them to a cage (kinda similar to jailbreak). When I do this though it seems that when someone is teleported it does not save their ban and when they leave and join again they are no longer in the cage. I have tried editing many things on the script though none have seemed to work. Also, this is a server script and I have a separate script making the value true or not and if true then it teleports them.
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("BanningSystem")
game.Players.PlayerAdded:Connect(function(plr)
local BanningSystem = Instance.new("BoolValue", plr)
BanningSystem.Name = "IsBanned"
local success, result = pcall(function()
ds1:GetAsync(plr.UserId)
end)
if not success then
warn(result)
else
if ds1:GetAsync(plr.UserId) then
BanningSystem.Value = ds1:GetAsync(plr.UserId)
else
BanningSystem.Value = false
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, plr.IsBanned.Value)
end)
With the SetAsync and GetAsync it needs to be a string. You will do this by using tostring.
Here is the fixed code:
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("BanningSystem")
game.Players.PlayerAdded:Connect(function(plr)
local BanningSystem = Instance.new("BoolValue", plr)
BanningSystem.Name = "IsBanned"
local success, result = pcall(function()
ds1:GetAsync(tostring(plr.UserId))
end)
if not success then
warn(result)
else
if ds1:GetAsync(plr.UserId) then
BanningSystem.Value = ds1:GetAsync(tostring(plr.UserId))
else
BanningSystem.Value = false
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(tostring(plr.UserId), plr.IsBanned.Value)
end)
So you don’t have to retrieve the data again. Because once you know the pcall is a success, you don’t use that successful data instead you just try to get Async again without a pcall which is probably why you can’t get your data.
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("BanningSystem")
game.Players.PlayerAdded:Connect(function(plr)
local BanningSystem = Instance.new("BoolValue", plr)
BanningSystem.Name = "IsBanned"
local playerdata
local success, result = pcall(function()
playerdata = ds1:GetAsync(plr.UserId)
end)
if not success then
warn(result)
else
if playerdata then
BanningSystem.Value = ds1:GetAsync(plr.UserId)
else
BanningSystem.Value = false
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, plr.IsBanned.Value)
end)
All I’ve simply changed is added a variable on the if statement and in the pcall.