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)
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.
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!
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.
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.