I'm trying to dispense a player an item, but the following function doesn't work. Any ideas why? It would be nice to explain it.
local sandwich = script.Parent["Turkey Sandwich"] local clicker = script.Parent.ClickDetector function GiveSandwich(playerWhoClicked) local players = game:GetService("Players") local human = players:FindFirstChild(playerWhoClicked.Name) if human then print("Successfully Found Player") local newsandwich = sandwich:Clone() newsandwich.Parent = human.Backpack print("Sandwich is dispensed and event completed") end end clicker.MouseClick:Connect(GiveSandwich)
Hello , First thing I noticed in your script was that you were trying to find the player's name from a service class game:GetService("Players") ... More information here ...
I replaced the whole concept of what you were trying to do , with tables
local sandwich = script.Parent["Turkey Sandwich"] local clicker = script.Parent.Clickdetector local human function GiveSandwich(playerWhoClicked) local players = game:GetService("Players") for i , v in pairs (players:GetChildren()) do if v.Name == playerWhoClicked.Name then -- can be replaced with characters and also player object itself... human = v end end if human then print("Successfully Found Player") local newsandwich = sandwich:Clone() newsandwich.Parent = human.Backpack print("Sandwich is dispensed and event completed") end end clicker.MouseClick:Connect(GiveSandwich)
Thank you for reading! Wish you goodluck for future scripts!