Help with my gamepass which changes the player's size

I have two scripts and a value in ReplicatedStorage.
firstly: in ReplicatedStorage there is a boolvalue called GrowOwn

in StarterGui I have a localScript inside of a button:

local MarketplaceService = game:GetService("MarketplaceService")
local gamePassId = 686197147
local button = script.Parent
local owned = false

local function onButtonClicked()
	local player = game.Players.LocalPlayer
	-- purchase
	if player then
		if not MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId) then
			local purchaseSuccess, purchaseResult = pcall(function()
				return MarketplaceService:PromptGamePassPurchase(player, gamePassId)
			end)

			if purchaseSuccess then
				-- Gamepass purchased successfully, perform any desired actions
				print("Gamepass purchased!")
				owned = true
			else
				-- Purchase failed or was cancelled
				print("Gamepass purchase failed or was cancelled.")
			end
		else
			-- Player already owns the gamepass
			print("Player already owns the gamepass.")
			owned = true
		end
	end

end

while true do
	if owned == true then
		game.ReplicatedStorage.GrowOwn.Value = true
	end
end

button.MouseButton1Click:Connect(onButtonClicked) 

Then in the workspace in a folder named “Benefits” there is a Script (NOT LOCAL)

local rs = game.ReplicatedStorage
local check = rs.GrowOwn

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")
while true do
	if check == true then
		humanoid.BodyWidthScale.Value = 1
		humanoid.BodyDepthScale.Value = 1
		humanoid.BodyHeightScale.Value = 1
		humanoid.HeadScale.Value = 1
		humanoid.JumpPower = 50
		humanoid.WalkSpeed = 18 -- def 16
		print("changed player stat")
	end
end

I am rather noobie when it comes to scripting so am unsure of how I can solve my issue.

Whenever I start the game and press the button it gives the following errors:
Workspace.Benefits.GrowBenefit:5: attempt to index nil with 'Character'

Script timeout: exhausted allowed execution time

i tried modifying the script for the growth but it was worse…

1.game.Players.LocalPlayer doesn’t work in a server script, it only works in localscripts.
2. Do you want the player to have a different size every time they join and respawn or only when they press the button?
3. I don’t think you can use MarketPlaceService on the client
4. The exhausted allowed execution time is because of your while loop in the local script

Instead, you should fire a remote event to the server. Inside of the server script, check if the player has the gamepass and then change their size.

The local script for the button:

local RemoteEvent = game.ReplicatedStorage.YourRemoteEvent

local debounce = true --makes sure that the player can't spam the button
local function onButtonClicked()
    if debounce then
        debounce = false
        RemoteEvent:FireServer()
        task.wait(2)
        debounce = true
    end
end)

script.Parent.MouseButton1Click:Connect(onButtonClicked)

Server script:

local MarketplaceService = game:GetService("MarketplaceService")
local RemoteEvent = game.ReplicatedStorage.YourRemoteEvent
local gamePassId = 686197147

game.Players.PlayerAdded:Connect(function(player)
    local GrowOwn = Instance.new("BoolValue", player)
    GrowOwn.Name = "GrowOwn"
    if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId) then
         GrowOwn.Value = true
    end
end)

RemoteEvent.OnServerEvent:Connect(function(player)
	local GrowOwn = player:FindFirstChild("GrowOwn")
	if GrowOwn then
		if GrowOwn.Value then --Same as doing GrowOwn.Value == true
			local Character = player.Character or player.CharacterAdded:Wait()
			local Humanoid = Character:FindFirstChild("Humanoid")
			if Humanoid then
				Humanoid.BodyWidthScale.Value = 1
				Humanoid.BodyDepthScale.Value = 1
				Humanoid.BodyHeightScale.Value = 1
				Humanoid.HeadScale.Value = 1
				Humanoid.JumpPower = 50
				Humanoid.WalkSpeed = 18 -- def 16
			end
		else
			local purchaseSuccess, purchaseResult = pcall(function()
				return MarketplaceService:PromptGamePassPurchase(player, gamePassId)
			end)
			if purchaseSuccess and purchaseResult then
				print("Bought gamepass")
				GrowOwn.Value = true
			else
				print("Gamepass purchase failed or was cancelled")
			end
		end
	end
end)

I probably made a small mistake somewhere in this

Also, sorry if this is confusing

1 Like

I only want it to happen when the player presses the button

I don’t know how to use RemoteEvents PROPERLY

1 Like

I strongly recommend learning how to use them at some point since you will need to use them often. They are basically just BindableEvents except it allows the server to fire the client and vice versa.

edit: nevermind

1 Like

I’ll try and will also try your code too.

1 Like

@PolyLacticSugarcane IT WORKED!

i’ll follow your advice and learn remote events!

1 Like

@PolyLacticSugarcane sorry, but all of a sudden (i haven’t modified anything) the script stopped working.

Whenever I click the button to grow in size, it pops up with:

Did you modify any of the scripts? I checked the script again and it seems fine, but I will check it again tomorrow when I am not as tired.

1 Like

I did not modify them (other than removing an error causing “)” at the end of the localscript. Note that I had done this before I tested it all the working times.)

1 Like

Maybe check if the GrowOwn value changes to true when you join the game. If it doesn’t, then it is an error with the playeradded part of the script.

1 Like
if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId) then
		GrowOwn.Value = true
		print("Grow is owned!")
	end

It does work

1 Like

It seems like it is an issue with the if GrowOwn.Value then part of the script. I’ll look at the script again some time tomorrow.

1 Like

I tried moving around things in the script but that didn’t seem to help so uh i’m not sure moving things around will solve much.

i could be wrong though.