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'
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 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.
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.)