Why is this not working?

So this script is meant to give a player if they have premium a special sniper, but it does not, I do not see any mistake but I am stupid and probably doing everything wrong:

Here is le code again probably doing 99% of it wrong:

local Players = game:GetService(“Players”)
local player = Players.LocalPlayer

if player.MembershipType == Enum.MembershipType.Premium then

game.ServerStorage.PremiumSniper:Clone().Parent = player:WaitForChild("Backpack")
game.ServerStorage.PremiumSniper:Clone().Parent = player:WaitForChild("StarterGear")

end

Formatting is not working sorry

Try FindFirstChild or WaitForChild("Child",3) instead.

Make sure its running the code.

1 Like

What I would suggest doing is first creating a variable for the sniper, then whenever a player with premium joins, create another variable for the cloned sniper and then parent the sniper to the player’s backpack or wherever you want it.

1 Like
  1. Create a Script
  2. Place in ServerScriptService
  3. Put the code in
local PremiumSniper = game.ServerStorage.PremiumSniper

game:GetService("Players").PlayerAdded:Connect(function(Player)
    if Player.MembershipType == enum.MembershipType.Premium then
        PremiumSniper:Clone().Parent = Player:WaitForChild("Backpack")
        PremiumSniper:Clone().Parent = Player:WaitForChild("StarterGear")
    end
end)
1 Like
--//Server Script\\--
local PremiumSniper = game.ServerStorage.PremiumSniper

game.Players.PlayerAdded:Connect(function(Player)
	if Player.MembershipType == Enum.MembershipType.Premium then
		local clone = PremiumSniper:Clone()
		clone.Parent = Player:WaitForChild("StarterGear")
	end
end)

I tried it and it works. (Also saves when player resets)

1 Like

Firstly, the client cannot access anything within ServerStorage, anything you try to access will return nil.
Here’s what I would do, in a server-sided script:

local Players = game:GetService('Players')
local ServerStorage = game:GetService('ServerStorage')
local PremiumSniper = ServerStorage.PremiumSniper


local function PlayerAdded(Player)
	if Player.MembershipType == Enum.MembershipType.Premium then
		local Backpack, StarterGear = Player:FindFirstChild('Backpack'), Player:FindFirstChild('StarterGear')
		if Backpack and StarterGear then
			PremiumSniper:Clone().Parent = Backpack
			PremiumSniper:Clone().Parent = StarterGear
		end
	end
end


local GetPlayers = Players:GetPlayers()
for i = 1, #GetPlayers do
	local Player = GetPlayers[i]
	PlayerAdded(Player)
end

Players.PlayerAdded:Connect(PlayerAdded)

Good luck on your project — and let me know if this was helpful if wanted.

2 Likes