I need some help with gamepass script

I’m a very very new scripter and I’m trying to script a gamepass for a sword!
The error I’m getting is. Sword is not a valid member of Script “ServerScriptService.Script”

Script

local MarketPlaceService = game:GetService(“MarketplaceService”)

local GamePassID = 20662961
local Tool = script.Sword

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)

I put the sword in ReplicatedStorage

1 Like

uh… You said you put the sword in rep storage but it’s also supposed to be under the script? If the sword is not under the script, move it there. Or you could do game.ReplicatedStorage.Sword.

1 Like

You need to do this:

local Sword = game:GetService('ReplicatedStorage'):WaitForChild('Sword')

Try that and see if that helps. Questions?

1 Like

Hi! Glad to see you’re taking an interest in programming!

Here’s where I think you’ve gone wrong, as well as a modified script that should work.

The parent of the tool is where it is stored, in this script you’ve noted that to be “script.Sword”, which would mean that the “Sword” is a child of “.” the script you’re writing this in. The dot basically means child of.

In this situation, you can either move the sword into the script, which should work. Or you can replace “script.Sword”, with game.ReplicatedStorage:FindFirstChild(“Sword”) which is where you’ve mentioned the sword is actually located.

Assuming nothing else is wrong with your script, here’s a modified version that should fix the issue you’re having:

local MarketPlaceService = game:GetService(“MarketplaceService”)

local GamePassID = 20662961
local Tool = game.ReplicatedStorage:FindFirstChild("Sword")

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)

Have fun, and let me know if you have any further questions!

2 Likes

In a normal situation, WaitForChild() and FindFirstChild() arent necessary

Hi!

I understand where you’re coming from, and yes that is true. However, in this situation I felt it might be important to add the waitforchild implementation as I was unsure whether or not they would move the item properly, and thus I didn’t want the rest of their game to freeze up in the Studio due to an error.

All the best :slight_smile:

Thank you for the script! But when I used your script I got this error.

ServerScriptService.Script:1: Expected identifier when parsing expression, got Unicode character U+201c (did you mean ‘"’?)

Do I have to change something?

That’s just a small typo. Search the line of the error. I think it’s the marketplace service line

local MarketPlaceService = game:GetService(“MarketPlaceService”)

I Capitalized the “P” in MarketPlaceService.

I can’t find anything can you?

i think its the quotes. MarketplaceService is just that i think

It should be


local MarketPlaceService = game:GetService("MarketPlaceService")

Your script assumes that the “sword” is the child of the script, but you stated its in ReplicatedStorage, try using:

local tool = game:GetService(“ReplicatedStorage”):WaitForChild(“Sword”)

1 Like

I get this error

Argument 1 missing or nil

This is not work :frowning:

what line is the error on, its most likely a parameter on a function that is nil

I think it’s the marketplace service line. it should be MarketplaceService

The first

local MarketPlaceService = game:GetService(MarketPlaceService)

and when I put the quotes back on this is what error I get.

‘MarketPlaceService’ is not a valid Service name

its MarketplaceService, if ur unsure of the capitalization you can look at the api

local MarketPlaceService = game:GetService("MarketplaceService")

Try that.

Solution to afakeusername as he pointed out the error first place.

As I mentioned in my ancillary reply, I did not check the rest of the script for typos or errors. Lua, and more specifically the Roblox framework can be very finicky when it comes to capitalization. Please make sure your script is free of typos and try running it again.

In short, I second this solution, but please contact me via DM if this fails to work for some unrelated reason.

No it is right how it is, it’s game:GetService("MarketplaceService")