So I’m learning to script and I want to make a sword gamepass and I used a YouTube video!
When I took the ClassicSword and cloned it the script did not work anymore. I know that it’s not the sword because it works when I put it in StarterPack.
Script
local MarketPlaceService = game:GetService(“MarketplaceService”)
local GamePassID = 20695006
local tool = game:GetService(“ReplicatedStorage”):WaitForChild(“ClassicSword”)
game.players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if MarketPlaceService:UserOwnsGamePassAsync(Player.UserID, GamePassID) then
local ToolClone = tool:Clone()
ToolClone.Parent = Player.Backpack
end
end)
end)
LocalScript
local player = game.Players.LocalPlayer
local ownsGamepass = game:GetService(“MarketplaceService”):UserOwnsGamePassAsync(player.UserId,20695006)
if ownsGamepass then
local sword = game:GetService(“ReplicatedStorage”):WaitForChild(“ClassicSword”):Clone()
sword.Parent = player.Backpack
end
Buy Prompt
script.Parent.MouseButton1Click:Connect(function()
game:GetService(“MarketplaceService”):PromptGamePassPurchase(game.Players.LocalPlayer,20695006)
end)
Your sword doesn’t work because you clone it on the client. You’d need to use a RemoteEvent (or RemoteFunction) to communicate to the server to properly clone the sword from the client on the server.
1 Like
Yes, thank you! But I very very new I know that you told me to ad a RemoteEvent but I don’t know what to name it. Thank you!
You must do it from a server script because the scripts will not work if you do it from a localscript and if you do it only the local player can see it. Also, if when the player dies, you want the tool not to be deleted from the player’s backpack, you can clone the tool and put it in StarterGear and also in the backpack.
Try to put UserId instead and check if it works.
1 Like
You don’t need to do this in a LocalScript, but you could if you’d like to. If you ever want something like clicking a UI button on the client to interact to the server - that’s when you use a RemoteEvent.
You can completely delete your LocalScript. As @AxeI_c mentioned, if you correct UserID to UserId in your Script, it should work. Currently your LocalScript “works” by giving you a non-functioning sword and your Script doesn’t work because of the small typo.
I changed “UserID” to “UserId” But nope! I must be not understanding anything. Also if I put it in a ServerScript and past the localscript into a ServerScript it does not show up. Thanks for help!
I got to go.
Are you receiving any errors in the output?
Also there’s one more typo in your Script. You do “game.players.PlayerAdded” - you need to either do game.Players (capital P) or game:GetService(“Players”). Capitalization is important.
I found another typo here:
It must be Players not players.
1 Like
So I would suggest you use :WaitForChild() since it would be better for your situation and try my code.
-- Gamepass Handler
local GamePassID = 20695006
local Players = game:GetService("Players")
local tool = game:GetService(“ReplicatedStorage”):WaitForChild(“ClassicSword”)
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, tonumber(GamePassID)) then
local ToolClone = tool:Clone()
ToolClone.Parent = Player:WaitForChild("Backpack")
end
end)
end)