Hi there!
I just started scripting and i am trying to script a buy shop menu.However i came across a problem.Whenever I purchase the item I am able to buy it.However when I print for a return it printed out nil.I was wondering what is the problem as I have scripted it to print out “true” if you buy it and “False” if something went wrong.However it only printed out nil.
Any help would be appreciated!
BuyTitle = function(plrName,ItemName)
local player = game.Players:FindFirstChild(plrName)
local inventory = player.OwnedTitles
local Tokens = player.PlayerValues.Tokens.Value
local Titles = game.ReplicatedStorage.Titles
local Item = Titles:FindFirstChild(ItemName)
local Price = Item.Price.Value
if Tokens >= Price then
local endtokens = Tokens - Price
Tokens = endtokens
local insert = Instance.new("StringValue")
insert.Name = Item.Name
insert.Value = Item.Name
insert.Parent = inventory
return true
else
return false
end
2 Likes
It would be good if you could show some more code, preferably the entire script, but I believe that you may be setting the value of the variable BuyTitle
instead of the callback function. Try replacing the top line with this instead:
BuyTitle.OnServerInvoke = function(plrName,ItemName)
Hey!Thanks for replying.
I forgot to mention that this code is in a module script.
local script:
BuyTitle.OnServerInvoke = function(player,ItemName)
local plyrname = player.Name
BuyModule.BuyTitle(plyrname,ItemName)
end
module script:
BuyTitle = function(plrName,ItemName)
local player = game.Players:FindFirstChild(plrName)
local inventory = player.OwnedTitles
local Tokens = player.PlayerValues.Tokens.Value
local Titles = game.ReplicatedStorage.Titles
local Item = Titles:FindFirstChild(ItemName)
local Price = Item.Price.Value
if Tokens >= Price then
local endtokens = Tokens - Price
Tokens = endtokens
local insert = Instance.new("StringValue")
insert.Name = Item.Name
insert.Value = Item.Name
insert.Parent = inventory
print("true")
return true
else
print("false")
return false
end
server script:
BuyTitle.OnServerInvoke = function(player,ItemName)
local plyrname = player.Name
BuyModule.BuyTitle(plyrname,ItemName)
end
I’m a little bit confused on one thing; that being that the value of the Local Script and the Server Script are the same
My bad!Accidentally pasted twice.
Heres the scripts:
local script:
local button = script.Parent
local BuyTitleRemote = game.ReplicatedStorage.Remotes.BuyTitle
button.MouseButton1Click:Connect(function()
local itemname = button.Parent.Name
if button.Name == "Buy" then
local result = BuyTitleRemote:InvokeServer(itemname)
print(result)
if result == true then
button.Name = "Equip"
end
elseif button.Name == "Equip" then
print("Equipping..")
elseif button.Name == "Unequip" then
print("Unequipping")
end
end)
server script:
BuyTitle.OnServerInvoke = function(player,ItemName)
local plyrname = player.Name
BuyModule.BuyTitle(plyrname,ItemName)
end
module script:
BuyTitle = function(plrName,ItemName)
local player = game.Players:FindFirstChild(plrName)
local inventory = player.OwnedTitles
local Tokens = player.PlayerValues.Tokens.Value
local Titles = game.ReplicatedStorage.Titles
local Item = Titles:FindFirstChild(ItemName)
local Price = Item.Price.Value
if Tokens >= Price then
local endtokens = Tokens - Price
Tokens = endtokens
local insert = Instance.new("StringValue")
insert.Name = Item.Name
insert.Value = Item.Name
insert.Parent = inventory
print("true")
return true
else
print("false")
return false
end
Ah! Thank you for the clarification. I believe that you forgot to add a return
statement on line 3 of the server script.
Try replacing line 3 with the following:
return BuyModule.BuyTitle(plyrname,ItemName)
1 Like
Oh!Thanks man it worked and returned true.Thanks for the help really!
1 Like