Anyone know why my script isn’t working? Nothing appears in the output.
Script:
local Players = game:GetService("Players")
local part = script.Parent
local MarketplaceService = game:GetService("MarketplaceService")
part.Touched:Connect(function(hit)
if not (Players:GetPlayerFromCharacter(hit.Parent)) then return end
local Player = Players:GetPlayerFromCharacter(hit.Parent)
local gamepass = 26096507
if MarketplaceService:UserOwnsGamepassAsync(Player.UserId, gamepass) then
local Find = Player.Backpack:FindFirstChild("WaterBottle")
if not Find then
local h = game.ReplicatedStorage.WaterBottle:Clone()
h.Parent = Player:WaitForChild("Backpack")
print("Bottle Given.")
else MarketplaceService:PromptGamePassPurchase(gamepass)
end
end
end)
Code within LocalScripts will only run if it’s a descendant of any of the following objects:
A Player’s Backpack , such as a child of a Tool
A Player’s Character model
A Player’s PlayerGui
A Player’s PlayerScripts .
The ReplicatedFirst service
Although you didn’t show the full hierarchy leading up to the LocalScript in the screenshot you posted, it appears as if it is not a descendant of any of the objects listed above, which means that the LocalScript’s code won’t run.
Converting that LocalScript to a Server Script should allow it to work as intended, running the function when that part is touched while also making sure that the action of giving the item to the player is replicated properly (seen by the server & other players).
local Players = game:GetService("Players")
local part = script.Parent
local MarketplaceService = game:GetService("MarketplaceService")
part.Touched:Connect(function(hit)
if not (Players:GetPlayerFromCharacter(hit.Parent)) then return end
local Player = Players:GetPlayerFromCharacter(hit.Parent)
local gamepass = 26096507
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepass) then
local Find = Player.Backpack:FindFirstChild("WaterBottle")
if not Find then
local h = game.ReplicatedStorage.WaterBottle:Clone()
h.Parent = Player:WaitForChild("Backpack")
print("Bottle Given.")
else MarketplaceService:PromptGamePassPurchase(gamepass)
end
end
end)
I also just noticed that this alternative condition is lined up with the incorrect conditional statement (which means that it would only be prompted if the player already owns the gamepass and does not have the item in their Backpack already:
In order to resolve this, the else statement should be moved outside of the end that closes off the “if not Find then” statement:
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepass) then
local Find = Player.Backpack:FindFirstChild("WaterBottle")
if not Find then
local h = game.ReplicatedStorage.WaterBottle:Clone()
h.Parent = Player:WaitForChild("Backpack")
print("Bottle Given.")
end
else
MarketplaceService:PromptGamePassPurchase(Player, gamepass)
end
local Players = game:GetService("Players")
local part = script.Parent
local MarketplaceService = game:GetService("MarketplaceService")
part.Touched:Connect(function(hit)
if not (Players:GetPlayerFromCharacter(hit.Parent)) then return end
local Player = Players:GetPlayerFromCharacter(hit.Parent)
local gamepass = 26096507
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepass) then
local Find = Player.Backpack:FindFirstChild("WaterBottle")
if not Find then
local h = game.ReplicatedStorage.WaterBottle:Clone()
h.Parent = Player:WaitForChild("Backpack")
print("Bottle Given.")
else MarketplaceService:PromptGamePassPurchase(Player, gamepass)
end
end
end)
Ok! I think i’ve found the solution to your problem. In your code:
if not Find then
local h = game.ReplicatedStorage.WaterBottle:Clone()
h.Parent = Player:WaitForChild("Backpack")
print("Bottle Given.")
else MarketplaceService:PromptGamePassPurchase(Player, gamepass)
end
You run the else statement if it doesn’t find the water bottle in the backpack, so a simple fix to that would be moving that else statement outside of that if statement so it would be:
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepass) then
local Find = Player.Backpack:FindFirstChild("WaterBottle")
if not Find then
local h = game.ReplicatedStorage.WaterBottle:Clone()
h.Parent = Player:WaitForChild("Backpack")
print("Bottle Given.")
end
else
MarketplaceService:PromptGamePassPurchase(Player, gamepass)
end