Hello! I have been trying to get this simple script to work but there is an issue that I am struggling to resolve. I have searched many websites and found nothing on this issue, including the documentaion on guis. When I printed plr it printed “nil”.
Here is the script.
local BuyButton = script.Parent
local ServerStorage = game:GetService("ServerStorage")
local Apple = ServerStorage.Food.Apple
BuyButton.MouseButton1Click:Connect(function(plr)
if plr.leaderstats.Money.Value >= 50 then
plr.leaderstats.Money.Value -= 50
local ClonedApple = Apple:Clone()
ClonedApple.Parent = plr.BackPack
end
end)
My goal is to have the “ClonedApple” to enter the players backpack after they pay. Thank you!
Isnt that already a local script? If it is, you dont need the player variable inside the function, just get the localplayer with game.Players.LocalPlayer
I have high hopes in this method, but how do I fire the event. I did BuyEvent:FireClient In a local script this did not work. Am I doing this incorrectly?
local BuyButton = script.Parent
local plr = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local BuyEvent = RS.BuyApple
BuyButton.MouseButton1Click:Connect(function()
BuyButton:FireServer()
end)
ServerScript:
local ServerStorage = game:GetService("ServerStorage")
local Apple = ServerStorage.Food.Apple
local RS = game:GetService("ReplicatedStorage")
local BuyEvent = RS.BuyApple
BuyEvent.OnServerEvent:Connect(function(plr)
if plr.leaderstats.Money.Value >= 50 then
plr.leaderstats.Money.Value -= 50
local ClonedApple = Apple:Clone()
ClonedApple.Parent = plr.BackPack
end
end)
local BuyButton = script.Parent
local plr = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local BuyEvent = RS.BuyApple
BuyButton.MouseButton1Click:Connect(function()
BuyEvent:FireServer()
end)
S_Side
local ServerStorage = game:GetService("ServerStorage")
local Apple = ServerStorage.Food.Apple
local RS = game:GetService("ReplicatedStorage")
local BuyEvent = RS.BuyApple
local Db = false
BuyEvent.OnServerEvent:Connect(function(plr)
if Db == true then
-- do something like fire server to client to ask them to wait or purchase failed
return
end
Db = true
if plr and plr:WaitForChild("leaderstats") then
if plr.leaderstats.Money.Value >= 50 then
plr.leaderstats.Money.Value -= 50
local ClonedApple = Apple:Clone()
ClonedApple.Parent = plr.Backpack
else print("not enough money") end
else
warn("Something went wrong :/")
end
task.wait(2)
Db = false
end)