What do you want to achieve? Keep it simple and clear!
I am trying to create a song ban system for my game.
What is the issue? Include screenshots / videos if possible!
It keeps returning banned songs as unbanned when I check the datastore whether the ban is true or false. I have set them to true however it keeps returning as not being a banned song. I have set it to false in the database as well.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried switching the two around so if banned == false then came before the true statement. I have also tried just using an else statement rather than elseif and still nothing.
Does anyone have any idea on how to fix this?
local DataStore = game:GetService("DataStoreService")
local SongBanDataStore = DataStore:GetDataStore('SongBanDataStore')
local Players = game:GetService("Players")
local Banned = nil
game.ReplicatedStorage.CheckSongStatus.OnServerEvent:Connect(function(UserThatPressedButton, SongID)
local SuccessMessage, ErrorMessage = pcall(function()
Banned = SongBanDataStore:GetAsync(SongID)
end)
if Banned == true then
game.ReplicatedStorage.SongIsBanned:FireClient(UserThatPressedButton)
print('SongNotBanned')
elseif Banned == false then
game.ReplicatedStorage.SongNotBanned:FireClient(UserThatPressedButton)
print('SongIsNotBanned')
else
game.ReplicatedStorage.SongNotBanned:FireClient(UserThatPressedButton)
print('SongIsNotBanned')
end
end)
local DataStore = game:GetService("DataStoreService")
local SongBanDataStore = DataStore:GetDataStore('SongBanDataStore')
local Players = game:GetService("Players")
local Banned = nil
game.ReplicatedStorage.CheckSongStatus.OnServerEvent:Connect(function(UserThatPressedButton, SongID)
local SuccessMessage, ErrorMessage = pcall(function()
Banned = SongBanDataStore:GetAsync(SongID)
end)
if Banned == true then
game.ReplicatedStorage.SongIsBanned:FireClient(UserThatPressedButton)
print(Banned..' That song is currently banned')
else
game.ReplicatedStorage.SongNotBanned:FireClient(UserThatPressedButton)
print(Banned.. 'That song is not currently banned')
end
end)
well not exactly, I wanted you to keep the if, elseif, else statements and then in else put print(banned) as I believe that since banned is not true nor false, it will head into else
but we need to see what banned is equal to before you can fix anything
okay so how do you currently have it to where you set up a banned song or a not banned song?
something like this you have?
--assume songid is == a string value, otherwise it will not work
local success, errorMessage = pcall(function()
SongBanDataStore:SetAsync(SongID, false) --setting a id to false = not banned
end)
Ok so admittedly I do not know what to do from here aside from making changes to see if anything helps.
instead of your pcall function, do:
--remove the banned local from outside of the onserver event
--replace your pcall function with this
local success, Banned, keyInfo = pcall(function()
return SongBanDataStore:GetAsync(SongID)
end)
if success then
print(Banned)
end
I just put it back and change local Banned = nil to local Banned = true(and tried it as false) and the script when I GetAsync is just returning the same thing local Banned at the top and won’t change it to what the datastore has saved
--when saving a banned song
local success, errormessage = pcall(function()
SongBanDataStore:SetAsync(TargetSongID.."Banned",true)
end)
if success then
print("Data successfully saved!")
else
print("There was an error when saving data")
end
--replace the pcall
local data
local success, errormessage = pcall(function()
data = SongBanDataStore:GetAsync(MusicID.."Banned")
end)
if success then
print("Data successfully retrieved!")
print("this is our data:", data)
else
print("There was an error when retrieving data")
warn(errormessage)
end
reply with all resulting prints for saving and retrieving data