so basically im trying to make an obby, and i would like it to have a bit of monetisation involved by giving players coils. im testing out a script i have where it prompts a pass when a player touches a part, it works perfectly fine. however, when i try to give the player its coil back when they die using a server script, it duplicates it infinitely from the replicated storage. im very close to finishing it and this is the last part.
code:
local mps = game:GetService("MarketplaceService")
local id = 1107699790
local coil = game.ReplicatedStorage.GravityCoil
game.Players.PlayerAdded:Connect(function(plr)
local own = mps:UserOwnsGamePassAsync(plr.UserId, id)
if own == true then
coil:Clone().Parent = plr.Backpack
end
local tool = plr.Character:FindFirstChild("GravityCoil")
wait(5)
if not tool then
coil:Clone().Parent = plr.Backpack
while true do
wait(0.1)
if plr.Character:FindFirstChild("Humanoid").Health == 100 and not tool then
coil:Clone().Parent = plr.Backpack
end
end
end
end)
local mps = game:GetService("MarketplaceService")
local id = 1107699790
local coil = game.ReplicatedStorage.GravityCoil
game.Players.PlayerAdded:Connect(function(plr)
local own = mps:UserOwnsGamePassAsync(plr.UserId, id)
if own then else return end
coil:Clone().Parent = plr.Backpack
end)
local mps = game:GetService("MarketplaceService")
local id = 1107699790
local coil = game.ReplicatedStorage.GravityCoil
game.Players.PlayerAdded:Connect(function(plr)
local own = mps:UserOwnsGamePassAsync(plr.UserId, id)
if own then
coil:Clone().Parent = plr:WaitForChild("Backpack")
coil:Clone().Parent = plr:WaitForChild("StarterGear")
end
end)
Place the tool in StarterGear instead of the Backpack but only once like so to achieve that kind of behavior. Each player has their own StarterGear folder so you can choose which players you want to spawn with the given tool(s).
local Players = game:GetService('Players')
local MarketplaceService = game:GetService('MarketplaceService')
local GravityCoil = game:GetService('ReplicatedStorage'):WaitForChild('GravityCoil')
local GamePassId = 0
local function AddPlayer(Player)
-- Selecting the second argument out of the `pcall` and validating whether or not it's `true`.
local UserOwnsGamePass = select(2, pcall(MarketplaceService.UserOwnsGamePassAsync, MarketplaceService, Player.UserId, GamePassId)) == true
if UserOwnsGamePass then
local Backpack = Player:FindFirstChildWhichIsA('Backpack')
-- If the backpack existed before StarterGear, give them the tool immediately.
if Backpack then
local Copy = GravityCoil:Clone()
Copy.Parent = Backpack
end
local StarterGear = Player:WaitForChild('StarterGear')
-- Make sure they respawn with the tool.
if StarterGear then
local Copy = GravityCoil:Clone()
Copy.Parent = StarterGear
end
end
end
-- Add players that existed before the server script started.
for _, Player in pairs(Players:GetPlayers()) do
AddPlayer(Player)
end
Players.PlayerAdded:Connect(AddPlayer)
Don’t forget to change GamePassId to the Game Pass ID.
Edit: i forgot to add in start of playerAdded function in CloneTool() player instance CloneToolfunction check is player has Tool if he has then it clones to inventory
Edit2: Now i tested it works!
local mps = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local id = 1107699790 -- Id
local coil = ReplicatedStorage.GravityCoil
function CloneTool(plr:Player)
local own = mps:UserOwnsGamePassAsync(plr.UserId, id)
if own then
coil:Clone().Parent = plr.Backpack
end
end
Players.PlayerAdded:Connect(function(plr)
local Char = plr.Character or plr.CharacterAdded:Wait()
local Hum = Char:FindFirstChildOfClass("Humanoid")
CloneTool(plr)
Hum.Died:Connect(function()
task.wait(5)
CloneTool(plr)
end)
end)