Hello!
I’m making a gamepass which once bought, give the player 150 health. Can someone please help me with my script? Thanks!
local MarketplaceService = game:GetService(“MarketplaceService”)
local GampassID = 18664894
local humanoid = game.Players:WaitForChild(“Humanoid”)
local function loadHealth(player)
game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded:connect(function()
local human = plr.Character:WaitForChild(“Humanoid”)
local character = player.Character
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, GampassID)
end)
if hasPass and human then
human.MaxHealth = 150
end
Well… If you need help, can you state the problem? By all means, if you’re asking for us to fix your code, we can… but it’s better to not spoonfeed people so that you can learn instead of just taking a cheat sheet with you and leaving. Better to get that knowledge, instead!!
So with that being said, can you instead ask a question? Are you getting errors? What seems to be the problem here?
You have two functions, and you’re missing your end) right above the last end, here.
Let’s go through this together:
In the very first line, you’re defining a connection function for when a player is added, with a parameter being that player.
In the second line, you’re doing the same, just for the character instead.
Now we’re calling a custom function in the third line with the passing parameters being the player. You only called ONEend), while that second function doesn’t necessarily ever have an end, therefore you need to wrap it in one more end).
Try to indent some of this where needed. This is the best I can do so if it does not work try something else.
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
loadHealth(player)
end)
end)
local MarketplaceService = game:GetService(“MarketplaceService”)
local GampassID = 18664894
local humanoid = game.Players:WaitForChild(“Humanoid”)
local function loadHealth(player)
game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded:connect(function()
local human = plr.Character:WaitForChild(“Humanoid”)
local character = player.Character
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, GampassID)
end)
end)
if hasPass and human then
human.MaxHealth = 150
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
loadHealth(player)
end)
end)
I don’t understand why when a player joins the game it loads a function that involves checking when a player joins, when your current error is fixed, this will stop your script from even running as when the player joins it checks for when the player joins the game again which since they would already be in would never happen
…No, there’s a specific connection that’s made with this function. This code is pre-initialized, meaning that it runs even before a player is in the game, so this does run when ANY player joins, really.