Data Store not saving the Player's Boolean Value - lua

I was making a game that when the Player dies, then the Player will get banned but isn't working (The part of Saving the Bool Value that checks if the player was banned already been banned). How i can fix that? No errors in output.
Code:
local DataStoreService = game:GetService("DataStoreService")
local BANS = DataStoreService:GetDataStore("BANNED")
game.Players.PlayerAdded:Connect(function(Player)
local Char = Player.CharacterAdded:Wait() or Player.Character
local Hum = Char:WaitForChild("Humanoid") or Char.Humanoid
local banornot = Instance.new("Folder")
banornot.Name = "Banned"
banornot.Parent = Player
local banned = Instance.new("BoolValue", banornot)
banned.Name = "Banido"
banned.Value = false
local PlayerId = Player.UserId
local BANIDO = BANS:GetAsync(PlayerId)
Hum.Died:Connect(function()
if not BANIDO then
BANS:SetAsync(PlayerId, banned.Value)
Player:Kick("You can't join the game.")
end
end)
end)
game.Players.PlayerAdded:Connect(function(Player)
local PlayerId = Player.UserId
local banidovalue = Player:WaitForChild("Banned"):WaitForChild("Banido").Value
local BANIDO = BANS:GetAsync(PlayerId, banidovalue)
if BANIDO then
banidovalue = true
if banidovalue == true then
Player:Kick("You can't join the game. Sorry you know the rules.")
else print("erro")
end
end
end)

There are multiple issues with this.
The main issue is that BANIDO will never change. It will always have the value false, no matter what you do. Why? The only use of SetAsync takes the BoolValue's value, which also does not change. So, it's always false. There is a point in which you "set" it to true, except you don't. You take the value, put it into a variable, and set the variable to true. The actual BoolValue doesn't change. Even if you were to fix that, you kick the user right after that, which makes it useless.
Second, you don't need a BoolValue. I don't know why you used it, so I can't explain this part.
Here is a commented version of an improved script, in which I go through all of the issues:
local DataStoreService = game:GetService("DataStoreService")
local BANS = DataStoreService:GetDataStore("BANNED")
game.Players.PlayerAdded:Connect(function(Player)
local Char = Player.CharacterAdded:Wait() or Player.Character
local Hum = Char:WaitForChild("Humanoid") --No "or Char.Humanoid" is needed, WaitForChild waits until it finds the thing which makes that part useless.
--We don't need a BoolValue
local PlayerId = Player.UserId
local BANIDO = BANS:GetAsync(PlayerId)
if BANIDO then --I moved the check from the other connection to this one. Do not connect to an event twice in the same script if the necessities are the same.
Player:Kick("You can't join the game. Sorry you know the rules.")
end
--I did not do a check like this: BANIDO == true
--This is because BANIDO is already a boolean, true or false, and you're essentially saying "if this is true, give me true, if it's false, give me false" which is just the value
Hum.Died:Connect(function()
if not BANIDO then
BANS:SetAsync(PlayerId, true) --true instead of banned.Value. We got rid of the BoolValue, and also, even if we didn't, it wouldn't change.
Player:Kick("You can't join the game.")
end
end)
end)

Related

"Attempt to call a nil value" with data stores

I called the moduleScript and I did everything correctly I think
I also tried with a normal script but it didnt work
Pls help I tried lots of things but nothing works Im creating a new game and I cant continue the game because the data doesnt save
Im putting so much text because if not I cant publish this :)
local PlayerStatManager = {}
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetChildren("DataPlayer")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Parent = player
stats.Name = "leaderstats"
local clicks = Instance.new("NumberValue")
clicks.Name = "Clicks"
clicks.Parent = stats
local coins = Instance.new("NumberValue")
coins.Name = "Coins"
coins.Parent = stats
local rebirths = Instance.new("NumberValue")
rebirths.Name = "Rebirths"
rebirths.Parent = stats
local id = player.UserId
clicks.Value = playerData:GetAsync(player.UserId)
end)
game.Players.PlayerRemoving:Connect(function(player)
local id = player.UserId
playerData:SetAsync(id, player.leaderstats.Clicks.Value)
end)
return PlayerStatManager
local playerData = DataStoreService:GetChildren("DataPlayer")
This line causes the problem.
First of all, GetChildren doesn't get a child of an instance, it returns an array-like table of all children. In that case, FindFirstChild makes more sense.
Well, even FindFirstChild isn't correct! DataStoreService shouldn't have any children - if it does, you shouldn't care about them. You probably wanted to use GetDataStore.
About the error message, it says that you're trying to call a nil value, which means that you're trying to call something that doesn't exist. It's this line that causes the error:
clicks.Value = playerData:GetAsync(player.UserId)
You are using GetChildren, which returns a table. playerData is a table. You're trying to get GetAsync and then call it. In the table, we don't have the key GetAsync (we only have number keys, because it's an array-like table). So you're trying to call a nil value.

"attepted to index nil with Humanoid"

local animationstomp = script.Animation
local user = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character
local human = char.Humanoid
user.InputBegan:Connect(function(input, proccesedevent)
if input.KeyCode == Enum.KeyCode.F then
local animationtrack = human:LoadAnimation(animationstomp)
animationtrack:play()
end
end
end)
this script keeps returning error "attempted to index nil with huamnoid" i have looked for solutions on dev forum and on stack overflow but i could not find anything so im going to post myself. anyone catch any erros?
"attempted to index nil with huamnoid"
What that means is that this script loaded before the player's character loaded in.
To fix this, you can use
local char = player.Character or player.CharacterAdded:Wait()
-- uses the player if it's already loaded in, or waits for the character to load in
Also make sure to use
local human = char:WaitForChild("Humanoid")
-- to make sure you wait till the humanoid gets added
Final script
local animationstomp = script.Animation
local user = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
user.InputBegan:Connect(function(input, proccesedevent)
if input.KeyCode == Enum.KeyCode.F then
local animationtrack = human:LoadAnimation(animationstomp)
animationtrack:play()
end
end)

My cash value is suppose to go up every minute but its not. (Roblox Studio lua error)

I was trying to re-edit the code over and over again but it still didn't work I've created the folder leader stats and when I play the game it shows that it's a part of the player. It says however that it isn't a valid member.
The other error says: Cash is not a valid member of Folder "Players.(players name).leaderstats"
It's because game.Players.PlayerAdded is an event which you're assigning to a variable.
Try this for the PlayerAdded script (you will need that cash add function in this script):
local players = []
game.Players.PlayerAdded:Connect(function(ply)
table.insert(players, ply)
end)
while true do
wait(60)
for i, v in ipairs(players) do
v:WaitForChild("leaderstats").Counter.Value += 1
end
end
I've written this from my memory as I am away from a PC that can test this code so best of luck!
while wait(60) do
for i, v in ipairs(game.Players:GetChildren()) do
if v:FindFirstChild("leaderstats") then
if v.leaderstats:FindFirstChild("Counter") then
v.leaderstats.Counter.Value += 1
end
end
end
end
You always need to make sure what you're using exists. If you want to avoid errors, I prefer using FindFirstChild instead of WaitForChild to not get it into an infinite wait incase it somehow doesn't load.
it looks like you forgot to create the leaerstats folder. Here is a fixed code:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Counter = Instance.new("IntValue")
Counter.Name = "Counter"
Counter.Parent = Leaderstats
while true do
task.wait(60)
Counter.Value += 1
end
end)
This will start counting time after player joins. If you want to increase counter value of all players at the same time, use this code:
local PlayersService = game:GetService("Players")
local Players = {}
PlayersService.PlayerAdded:Connect(function(player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Counter = Instance.new("IntValue")
Counter.Name = "Counter"
Counter.Parent = Leaderstats
table.insert(Players, player)
end)
while true do
task.wait(60)
for _, player in ipairs(Players) do
player.leaderstats.Counter.Value += 1
end
end
You don't need to check if "Counter" or "leaderstats" exist as they are created before the player is being inserted into the table.

Set username/userID to boat when seat is occupied

I am working on a game in which you can sail with boats. My script works but I wonder if I am doing it right?
What I'm trying to do is that as soon as you spawn a boot the name of the boot is the userID + the name of the ship.
This works fine thanks to one guy on this forum (Much Love)!
So now I wanted to write a script inside the seat as soon as another player or the player himself sits on the chair changes the name of the boat to that player name + boat name. This works, but now I have managed to get the name of the player and not his id. I prefer the ID of the player. How do you do that?
Because I get the player name instead of his id. I had to make another check to see if the boat does not already exist. Now 1 player can get 2 boats because player.Name and player.UserID do not match.
So my question is how do I get the userID instead of its name from the script below so that a player can take over the boat but not get 2 boats in the end?
I'm sorry for my bad coding i'm new to this :) Thanks for helping out!
Script inside of seat:
local Seat = script.Parent
local debounceCheck = false
local ChangeBoatName = script.Parent.Parent
local boatname = "ShipOnePerson"
Seat.Changed:Connect(function(player)
if Seat.Occupant ~= nil then
if Seat.Occupant.Parent.Name ~= "PlayerNameHere" then
local PlayerToNewBoat = game.Players:FindFirstChild(Seat.Occupant.Parent.Name)
local Character = PlayerToNewBoat.Character or PlayerToNewBoat.CharacterAdded:Wait()
if not debounceCheck then
-- Is it Occupant?
debounceCheck = true
-- Mark it as Occupant, so that other handlers don't execute
if ChangeBoatName then
local plruserid = Seat.Occupant.Parent.Name
local SetNameToBoat = plruserid..boatname
local NewBoat = ChangeBoatName
NewBoat.Name = SetNameToBoat
NewBoat.Parent = game.Workspace
print(NewBoat)
end
end
else
debounceCheck = false
-- Mark it as not Occupant, so other handlers can execute again
end
else
debounceCheck = false
-- Mark it as not Occupant, so other handlers can execute again
end
end)
My second question is how can I make sure that as soon as you press the button RegenButton it first checks if there is already a boat in that place and if so how do I make sure that you cannot spawn?
Below is the code that is in the button regenbutton
local cd = workspace.Regenbutton:WaitForChild('ClickDetector')
local boat = game:GetService('ServerStorage'):WaitForChild('ShipOnePerson')
local button = workspace:WaitForChild('Regenbutton')
local debounce = false
local boatname = "ShipOnePerson"
cd.MouseHoverEnter:Connect(function()
button.Transparency = 0.5
end)
cd.MouseHoverLeave:Connect(function()
button.Transparency = 0
end)
cd.MouseClick:Connect(function(player)
local plruserid = player.UserId
local SetNameToBoat = plruserid..boatname
local plrextraboat = player.Name..boatname
print(SetNameToBoat)
local oldboat = workspace:FindFirstChild(SetNameToBoat)
local oldboat2 = workspace:FindFirstChild(plrextraboat)
if not debounce then
if oldboat then
oldboat:destroy()
end
if oldboat2 then
oldboat2:destroy()
end
debounce = true
local NewBoat = boat:Clone()
NewBoat.Name = SetNameToBoat
NewBoat.Parent = game.Workspace
wait(5)
debounce = false
end
end)
So my question is how do I get the userID instead of its name
Instead of Player.Name use Player.UserId
My second question is how can I make sure that as soon as you press the button RegenButton it first checks if there is already a boat in that place and if so how do I make sure that you cannot spawn?
You could use Instance.GetChildren to get a list of all children, then iterate over that list in a loop. Check wether any of those children is a boat Instance.IsA and if so, check whether it is in the spawn location.

What does "attempt to index nil with 'WaitForChild'" mean?

script.Parent.MouseButton1Click:connect(function()
local RS = game:GetService("ReplicatedStorage")
local item = RS:WaitForChild("Pencil")
local price = 350
local player = game.Players.LocalPlayer
local stats = player:WaitForChild("leaderstats")
if stats.Strength.Value>=price then
stats.Strength.Value = stats.Strength.Value - price
local cloned = item:Clone()
cloned.Parent = player.Backpack
cloned.Parent = player.StarterGear
end
end)
I am trying to make a shop and it comes up with "attempt to index nil with 'WaitForChild'" on line 6:
local stats = player:WaitForChild("leaderstats")
I copied it exactly how the video had it and the video had no problem and apparently player has no value even though we set it up just one line above
It means that you are indexing a nil value, which means that player value is nil, which means that game.Players.LocalPlayer on the previous like returns nil. Why that is you need to figure out, as there is not much to go by.
The example shows that it should be local player = game:GetService("Players").LocalPlayer, so you may want to try that.

Resources