My script for checking a banned song isn't currently working. Does anyone have any idea why

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to create a song ban system for my game.
  2. 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.
  3. 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)

It keeps sending a notification that it isn’t banned when it is

I believe when you getasync the songid that it is still nil or atleast not true or false
try printing the variable banned in the else block

Alright. I will try that. Thanks for a quick reply.

Is this what you mean?

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

reply with the result of the print banned

Ah right. Ok I will revert it back.

It has returned back as nil in the output so yeah you were right
image

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)

Mhm

local SuccessMessage, ErrorMessage = pcall(function()
				SongBanDataStore:SetAsync(TargetSongID, true)
			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

That sadly doesn’t work. Doesn’t print or do anything

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

can try this instead though might not even work:

--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

Alright. Thanks. I will let you know the outcome in a bit as I have just gone out.

1 Like

just ensure that the ID inputted into the getasync and setasync is a stringvalue not numbers they also need to be identical to eachother

so one cannot be say “1893245” or integer 1893245 (there is big difference between these two)

and the other:

“rbxassetid://1893245”

for good measure you can always print the incoming ID and see if they are ==, you can also do tostring(musicid) if not sure if its a string or not

Ah right. Ye I think that’s where it is going wrong. I will just check now

Edit: I recreated a system and it now works completely fine,

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.