Not gonna lie, my language is C#, not Lua, so I've just yolo'd this project and I need help with something. I'm making a game (obv), and I need to make it where the player can buy this AR when they click it. Here's my code that isn't working:
Player = game.Players.LocalPlayer script.Parent.ClickDetector.MouseClick:Connect(function() if Player.LeaderBoard.Money.Value >= 500 then Player.LeaderBoard.Money.Value = Player.LeaderBoard.Money.Value - 500 game.ReplicatedStorage.M18:clone().Parent = Player:WaitForChild("Backpack") else end end)
Please help.
Hello.
Problem:
LocalPlayer
in a ServerScript. This will not work as LocalPlayer
only works on LocalScripts
.Solution:
player
argument of ClickDetector.MouseClick
instead of LocalPlayer
.Recommendations:
Use Clone()
with an uppercase "C" as clone()
is deprecated. It is never a good idea to use deprecated functions.
You do not have to use WaitForChild()
on the player's backpack as it will be loaded in.
Use GetService()
when indexing services as it checks if the service is existent. If it isn't it creates the service. GetService()
is also useful if someone renamed the services. GetService()
is the only way to create services.
You do not need the else
for the if statement on line 02.
Fixed Script:
script.Parent.ClickDetector.MouseClick:Connect(function(Player) if Player.LeaderBoard.Money.Value >= 500 then Player.LeaderBoard.Money.Value = Player.LeaderBoard.Money.Value - 500 game:GetService("ReplicatedStorage").M18:Clone().Parent = Player.Backpack end end)
Also, put local
before declaring a variable unless you're using a global variable. Local variables are faster and more efficient than global ones though.