Hi there, I’m kind of new with scripting (in fact, I’m not really good at it.) and I wanted to make a script where you type something like “/goldenkolibri” in chat, and if you have a gamepass that’s required, it would give it to you. Otherwise, if you don’t have the pass, it wouldn’t work (optionally, put up a prompt for them to purchase the gamepass).
Slight problem: I kind of suck.
Here’s what I’ve had so far.
function onChatted(msg, speaker)
local source = string.lower(speaker.Name)
local msg = string.lower(msg)
game.Players.PlayerAdded:connect(function(player)
if game:GetService("GamePassService"):PlayerHasPass(player, 18361943) then
player.Chatted:connect(function(msg) onChatted(msg, player)
if msg == '/goldenkolibri' then
local tool = game.Lighting.Gamepass:FindFirstChild("Golden Kolibri")
tool:Clone().Parent = player.Backpack
end
end)
end)
Let me know if there’s anything wrong, and if the script up here makes you cringe like crazy, I apologize sincerely. I just want to make this work, and I’m not that good.
Something definitely looks wrong here. It looks like the PlayerAdded event is in a function, and the event is also calling the same function. This would then be recursive, which isn’t good for this use case. You can fix the code like this:
local gps = game:GetService("GamePassService")
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
msg = msg:lower()
if msg:match("^/goldenkolibri") then
if gps:PlayerHasPass(player, 18361943) then
local tool = game.Lighting.Gamepass:FindFirstChild("Golden Kolibri")
if tool then tool:Clone().Parent = player.Backpack end
else
-- you could prompt the user to buy the item here if you want
end
end
end)
end)
--Script
local GamepassService = game:GetService("GamePassService")
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
msg = msg:lower()
if msg == "/goldenkolibri" and GamepassService:PlayerHasPass(player,18361943) then
local tool = game.Lighting.Gamepass:FindFirstChild("Golden Kolibri")
if tool then
local Clone = tool:Clone()
Clone.Parent = player.Backpack
end
end
end)
end)