Premium only tool error

Hello! Me and my friend decided to add premium benefits to our game. We decided that premium players will get an exclusive premium only tool (They have to click a part to get it).
The script doesn’t seem to work and I don’t know why.

This is a regular script I use for the regular tools. It gives players a tool once they click the part. It works perfectly.

local ToolNames = {"PremiumBlade", "Item", "Item"}
local Storage = game:GetService("ServerStorage")


local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")

ClickDetector.MouseClick:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool and not Backpack:FindFirstChild(Tool.Name) then
				Tool:clone().Parent = Backpack
			end
		end
	end
end)

This is for the premium only tool. It is broken and I don’t know why.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
 
if player.MembershipType == Enum.MembershipType.Premium then
	local ToolNames = {"PremiumBlade", "Item", "Item"}
local Storage = game:GetService("ServerStorage")


local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")

ClickDetector.MouseClick:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool and not Backpack:FindFirstChild(Tool.Name) then
				Tool:clone().Parent = Backpack
			end
		end
	end
end)

Thank you for reading.

1 Like

You seem to be using a local script in the last script. The contents of ServerStorage are inaccessible to clients. Do the checks for membership on the server script.

ClickDetector.MouseClick:Connect(function(Player)
	if Player.MembershipType == Enum.MembershipType.Premium then
		local Backpack = Player.Backpack
		for _, ToolName in ipairs(ToolNames) do
			local Tool = Storage:FindFirstChild(ToolName)
			if Tool and not Backpack:FindFirstChild(ToolName) then
				Tool:Clone().Parent = Backpack
			end
		end
	end
end)

How exactly would I add that in? I’m a newbie when it comes to scripting. Do I put this before all the code in the script? In which line should I put it?

Also, I am almost sure that I used a regular script, not a localscript. But I may be wrong.

Oh my bad i probably misread something. LocalPlayer is nil to the server since the server has no concept of “local player”. The script I sent you is then premium only one

That is because tool names is not in the same block of code in the script:

if player.MembershipType == Enum.MembershipType.Premium then
    local ToolNames = {"PremiumBlade", "Item", "Item"}
end -- you forgot an end here, so I assume there was an end here, so I put it in

So I would suggest doing this:

local ToolNames

if player.MembershipType == Enum.MembershipType.Premium then
    ToolNames = {"PremiumBlade", "Item", "Item"}
end

The second problem in your script is this part:

local Storage = game:GetService("ServerStorage")

Local scripts (I assume this is one, because LocalPlayer was in one of the variables) cannot access anything in the ServerStorage and ServerScriptService. So to get the player, I siggest using a PlayerAdded event:

game.Players.PlayerAdded:Connect(function(player)

This part is different, it’s not an error, it’s just a nitpick:

Instead of doing this:

for i = 1, #ToolNames do
    local Tool = Storage:FindFirstChild(ToolNames[i])

Do this:

for i, v in pairs(ToolNames) do
	local Tool = Storage:FindFirstChild(v)

So, your code would look something like this:

local Players = game:GetService("Players")
local Storage = game:GetService("ServerStorage")
local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")

Players.PlayerAdded:Cnnect(function(Player)

    local ToolNames

    if player.MembershipType == Enum.MembershipType.Premium then
	    ToolNames = {"PremiumBlade", "Item", "Item"}
    end

    ClickDetector.MouseClick:Connect(function()
	    if Player and Player.Character then
	        local Backpack = Player:WaitForChild("Backpack")
	        for i, v in pairs(ToolNames) do
		        local Tool = Storage:FindFirstChild(v)
		        if Tool and not Backpack:FindFirstChild(Tool.Name) then
				    Tool:Clone().Parent = Backpack
			    end
		    end
	    end
    end)
end)
3 Likes

let me check if it works! Let’s hope for the best.

It does not seem to work. Premium members press it but They don’t get the tool

Can you record a video of what is happening? And did you make sure that script was a server script? Try putting print commands in places to debug the script, if one print command doesn’t work, then you know that the problem lies close to the print command that didn’t print.


If there is an error, try fixing it.

It’s kinda hard to record a video for me, since something is glitching. I think I have found a solution for the problem anyways. Thank you for the help though.

1 Like

If you have a solution, it is best to either mark my first post as solution, or try replying to this topic with the solution and mark it as solution. This will let people know that there was a solution to this topic, and know which post to look at if they have the same problem.