Hello Devs so im trying to make when a player joins and owns the gamepass they get the tool. But for some Reason my code wont work. Its a local Script and is in the Starterplayerscripts Here is the code:
local player = game.Players.LocalPlayer
local ownsGamepass = game:GetService("MarketplaceService")
local Rep = game:GetService("ReplicatedStorage")
local BOomBox = Rep:WaitForChild("BoomBox")
local GamePassID = 17842792
game.Players.PlayerAdded:Connect(function()
if ownsGamepass:UserOwnsGamePassAsync(player, GamePassID ) then
BOomBox:Clone()
BOomBox.Parent = player.Character
end
LocalPlayer doesn’t work in server-side scripts. Why not define the player as the parameter of the PlayerAdded event?
game:GetService("Players").PlayerAdded:Connect(function(player)
-- And use the player with it
end)
I don’t recommend cloning it from ReplicatedStorage since it can be cloned by exploiters. You can move it to ServerStorage.
And also you should use the ID number of the player, as @LegendOJ1 mentioned.
So here is your revised script stored in ServerScriptService.
local ownsGamepass = game:GetService("MarketplaceService")
local Server = game:GetService("ServerStorage")
local BOomBox = Server:WaitForChild("BoomBox")
local GamePassID = 17842792
game:GetService("Players").PlayerAdded:Connect(function(player)
if ownsGamepass:UserOwnsGamePassAsync(player.UserId, GamePassID) then
local clone = BOomBox:Clone()
clone.Parent = player.Character
end
end)
Here is my advice.
You can use the GetService function instead of passing the raw form of the name of the service so this will help you a lot.
And instead of the ownsGamepass variable, you can change it to MarketplaceService service because every time you use that function, with the old variable your mind won’t be in confusion how you can use that variable and you don’t need to look above again if you forgot what it defines.
You should not be doing this through a local script for numerous reasons.
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- You could place it inside ServerStorage as well
local Players = game:GetService("Players")
local BoomBox = ReplicatedStorage:WaitForChild("BoomBox")
local GAMEPASS_ID = 17842792
Players.PlayerAdded:Connect(function(player)
if MarketplaceService:UserOwnsGamePassAsync(player.UserId, GAMEPASS_ID) then
player.CharacterAdded:Connect(function(character)
BoomBox:Clone().Parent = player.Character
end)
if player.Character then
BoomBox:Clone().Parent = player.Character
end
end
end