So I was making a shop but the buttons aren’t working. The buttons are in a folder named Buyables. When you click on them nothing happens.
Here is my code;
– Local Script
local Rank = script.Parent.Name
script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.Events.BuyRankEvent:FireServer(Rank)
end)
–Server Script
game.ReplicatedStorage.Events.BuyRankEvent.OnServerEvent:Connect(function(plr, Rank)
if Rank == "Private" then
if plr.leaderstats.Credits.Value >= 49 and not plr.leaderstats.Rank.Value == "Private" then
plr.leaderstats.Credits.Value -= 50
plr.leaderstats.Rank.Value = "Private"
end
elseif Rank == "Corporal" then
if plr.leaderstats.Credits.Value >= 149 and not plr.leaderstats.Rank.Value == "Corporal" then
plr.leaderstats.Credits.Value -= 150
plr.leaderstats.Rank.Value = "Corporal"
end
elseif Rank == "Sergeant" then
if plr.leaderstats.Credits.Value >= 399 and not plr.leaderstats.Rank.Value == "Sergeant" then
plr.leaderstats.Credits.Value -= 400
plr.leaderstats.Rank.Value = "Sergeant"
end
elseif Rank == "Gunery Sergeant" then
if plr.leaderstats.Credits.Value >= 899 and not plr.leaderstats.Rank.Value == "Gunery Sergeant" then
plr.leaderstats.Credits.Value -= 900
plr.leaderstats.Rank.Value = "Gunery Sergeant"
end
elseif Rank == "Sergeant Major" then
if plr.leaderstats.Credits.Value >= 1999 and not plr.leaderstats.Rank.Value == "Sergeant Major" then
plr.leaderstats.Credits.Value -= 2000
plr.leaderstats.Rank.Value = "Sergeant Major"
end
elseif Rank == "Lieutenant" then
if plr.leaderstats.Credits.Value >= 4999 and not plr.leaderstats.Rank.Value == "Lieutenant" then
plr.leaderstats.Credits.Value -= 5000
plr.leaderstats.Rank.Value = "Lieutenant"
end
elseif Rank == "Fleet Admiral" then
if plr.leaderstats.Credits.Value >= 19999 and not plr.leaderstats.Rank.Value == "Fleet Admiral" then
plr.leaderstats.Credits.Value -= 20000
plr.leaderstats.Rank.Value = "Fleet Admiral"
end
end
end)
game.ReplicatedStorage.Events.BuyRankEvent.OnServerEvent:Connect(function(plr, Rank)
if Rank == "Private" then
if plr.leaderstats.Credits.Value >= 49 and plr.leaderstats.Rank.Value ~= "Private" then
plr.leaderstats.Credits.Value -= 50
plr.leaderstats.Rank.Value = "Private"
end
elseif Rank == "Corporal" then
if plr.leaderstats.Credits.Value >= 149 and plr.leaderstats.Rank.Value ~= "Corporal" then
plr.leaderstats.Credits.Value -= 150
plr.leaderstats.Rank.Value = "Corporal"
end
elseif Rank == "Sergeant" then
if plr.leaderstats.Credits.Value >= 399 and plr.leaderstats.Rank.Value ~= "Sergeant" then
plr.leaderstats.Credits.Value -= 400
plr.leaderstats.Rank.Value = "Sergeant"
end
elseif Rank == "Gunery Sergeant" then
if plr.leaderstats.Credits.Value >= 899 and plr.leaderstats.Rank.Value ~= "Gunery Sergeant" then
plr.leaderstats.Credits.Value -= 900
plr.leaderstats.Rank.Value = "Gunery Sergeant"
end
elseif Rank == "Sergeant Major" then
if plr.leaderstats.Credits.Value >= 1999 and plr.leaderstats.Rank.Value ~= "Sergeant Major" then
plr.leaderstats.Credits.Value -= 2000
plr.leaderstats.Rank.Value = "Sergeant Major"
end
elseif Rank == "Lieutenant" then
if plr.leaderstats.Credits.Value >= 4999 and plr.leaderstats.Rank.Value ~= "Lieutenant" then
plr.leaderstats.Credits.Value -= 5000
plr.leaderstats.Rank.Value = "Lieutenant"
end
elseif Rank == "Fleet Admiral" then
if plr.leaderstats.Credits.Value >= 19999 and plr.leaderstats.Rank.Value ~= "Fleet Admiral" then
plr.leaderstats.Credits.Value -= 20000
plr.leaderstats.Rank.Value = "Fleet Admiral"
end
end
end)
You should add a print in your mouseClick to make sure that is firing and a print in your serverscript that prints the rank that was sent to ensure that was fired. To me the two most likely errors are that you are sending a string that’s not capitilized correctly so it never enters the intended branch or the events aren’t actually being created.
Your code has a lot of repetition though which usually means it can be done better. In this case you can condense it down in to this
local ranks = {
["Private"] = 50,
["Corporal"] = 150,
["Sergeant"] = 400,
["Gunery Sergeant"] = 900,
["Sergeant Major"] = 2000,
["Lieutenant"] = 5000,
["Fleet Admiral"] = 20000,
}
game.ReplicatedStorage.Events.BuyRankEvent.OnServerEvent:Connect(function(plr, Rank)
if ranks[Rank] and plr.leaderstats.Credits.Value >= ranks[Rank] and plr.leaderstats.Rank.Value ~= Rank then
plr.leaderstats.Credits.Value -= ranks[Rank]
plr.leaderstats.Rank.Value = Rank
end
end)
This is just more manageable since you only have to add a new line to ranks to add new stuff. Also when you need to adjust your function, you only have to do it in one place rather than in every branch