DataStore Not working

Hey can I get some help me once again becausepretty sure you cant use them in localscripts and thats why its not working but I have no idea how to fix that problem so some help would be nice

local DataStore = game:GetService("DataStoreService"):GetDataStore("BannedDataStore")
game.ReplicatedStorage.BanPopUp.OnServerEvent:Connect(function(player, Text)
    local player = game.Players:GetUserIdFromNameAsync(Text)
    local player2 = game.Players:FindFirstChild(Text)
    if player2 then
        DataStore:SetAsync(player2, true)
        player2:Kick()
    end
end)

then here is the local

script.Parent.Parent.BanButton.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.BanPopUp:FireServer(script.Parent.Text)
end)
 

Everything else works except saving the playerdata people told me the way to go was :Setasync

1 Like

Your scripts look good but always use pcall when setting a datastore value, because when you write over datastore values, they can fail sometimes and you could lose data since internal web requests fail sometimes.

And yes, datastore functions only work on the server.

Your code should look like this on the server:

local DataStore = game:GetService("DataStoreService"):GetDataStore("BannedDataStore")
game.ReplicatedStorage.BanPopUp.OnServerEvent:Connect(function(player, Text)
    local player = game.Players:GetUserIdFromNameAsync(Text)
    local player2 = game.Players:FindFirstChild(Text)
    if player2 then
        repeat -- This is the part I changed where you use pcall
	         local success, error = pcall(function()
		         DataStore:SetAsync(player2, true)
		         player2:Kick()
	          end)
        until success == true
    end
end)

It still doesnt ban them, how do I fix that?

I just realized I made a small mistake. Try putting the player2:Kick() outside of the pcall.

local DataStore = game:GetService("DataStoreService"):GetDataStore("BannedDataStore")
game.ReplicatedStorage.BanPopUp.OnServerEvent:Connect(function(player, Text)
    local player = game.Players:GetUserIdFromNameAsync(Text)
    local player2 = game.Players:FindFirstChild(Text)
    if player2 then
        repeat -- This is the part I changed where you use pcall
	         local success, error = pcall(function()
		         DataStore:SetAsync(player2, true)
	          end)
        until success == true
    player2:Kick()
    end
end)

It still doesnt ban them idk why would you like a copy of the place?

To make a Ban I would recommend using a boolean Value, then saving It.
You may insert this script in server script service

local DSService = game:GetService("DataStoreService")
local BannedDS = DSService:GetDataStore("BannedDataStore")

game.Players.PlayerAdded:Connect(function(plr)

local BannedValue = Instance.new("BooleanValue")
BannedValue.Parent = plr
BannedValue.Name = "BannedValue"

local PlrId = tostring(plr.UserId)

local sucess, problem = pcall(function()

if sucess then

local IsBanned = BannedDS:GetAsync(PlrId)
BannedValue.Value = IsBanned
end
end)


If BannedValue.Value == true then

plr:Kick("Your Banned")

end


end)


game.Players.PlayerRemoving:Connect(function(plr)

local BannedValue = plr:WaitForChild("BannedValue")

local PlrId = tostring(plr.UserId)

local sucess, problem = pcall(function()

if sucess then

BannedDS:SetAsync(PlrId,BannedValue.Value)

end

end)


end)

I’ll assume that “Text” is the banned player.
You can change your OnServerEvent script to that :

game.ReplicatedStorage.BanPopUp.OnServerEvent:Connect(function(player, Text)
    local player2 = game.Players:FindFirstChild(Text)
    local BannedValue = player2:WaitForChild("BannedValue")
        player2:Kick(Youve been Banned)
    end
end)

Hope It helped.

I recommend just using ProfileService, which will handle all your needs.
Here is a good tutorial about it.