In my game, I have a GUI which shows a Players 'Stamina'. This all works fine, but there's one thing I can't get to work. I have this script which 'upgrades' their Stamina when they have a certain Gamepass. Personally, I think the Server can't see what's inside a Players PlayerGui and that is why the script does not work.

So my question: Can someone show me how to (if not mistaken that's how to) use RemoveEvents to Destroy the current script inside the GUI (=StaminaHandler, local) and replace it with the new script (=StaminaDrug, local) ? Thanks in advance!

Script in workspace

local player = game:GetService("Players").LocalPlayer
local passID = 1465710522
local BoostScript = script:WaitForChild("StaminaDrug")

game:GetService("MarketplaceService").PromptPurchaseFinished:Connect(function(player, passID, isPurchased)
	if isPurchased then
		local AwardedPlayer = player.PlayerGui:WaitForChild("LivesGui").ImageLabel.Stamina
		if AwardedPlayer then
			script.Sound:Play()
			AwardedPlayer.StaminaHandler:Destroy()
			BoostScript:Clone().Parent = AwardedPlayer
		end
	end
end)

game:GetService("Players").PlayerAdded:Connect(function(joined)
	joined.CharacterAdded:Connect(function(loaded)
		if game:GetService("MarketplaceService"):PlayerOwnsAsset(joined, passID) then
			local AwardedPlayer = joined.PlayerGui:WaitForChild("LivesGui").ImageLabel.Stamina
			if AwardedPlayer then
				AwardedPlayer.StaminaHandler:Destroy()
				BoostScript:Clone().Parent = AwardedPlayer
			end
		end
	end)
end)

1 comment

0

why not just use an if and elseif statement in the original stamina script to change their stamina if they have the gamepass?