Basically I’m making a button where if you own a gamepass you can enable something called gode mode where you can’t die from parts that kill you (I’m making an obby)
I have a script in ServerScriptService that tells the Kill parts to kill the humanoid:
local KillBricksFolder = game.Workspace.KillParts
local EasyModeButton = game.StarterGui.EasyMode.EasyModeButton
for _, KillBrick in pairs(KillBricksFolder:GetChildren()) do
KillBrick.Touched:Connect(function(touchPart)
local humanoid = touchPart.Parent:FindFirstChild("Humanoid")
if humanoid then humanoid.Health = 0
end
end)
end
So what I’m trying to do is make a gamepass called god mode where you can enable god mode when you press a textbutton. I want the script above to check if the textbutton says says something specific like “God Mode Enabled”
Next I need to know how I can code the button to where if you press the button it checks if you own the gamepass. If you own the gamepass then it will change the text. If you don’t own the gamepass it will prompt you to buy the gamepass and the text will not change.
Can someone type the script out and explain it to me do I can understand what to do? I’m trying to learn how to script and I want to learn.
local Player = game.Players.LocalPlayer
local Market = game:GetService("MarketplaceService")
local Button = script.Parent
local RemoteEvent = game.ReplicatedStorage.GodMode -- you can change "game.replicatedStorage" to wherever the event is
if Market:UserOwnGamepassAsync(Player.UserId,[[Gamepass id Goes here]]) then
Button.Visible = true
else
Button.Visible = false
end
Button.MouseButton1Click:Connect(function()
RemoteEvent:FireServer()
end)
For the event handling, have a script somewhere, I’d probably put it in Workspace.
game.Players.PlayerAdded:Connect(function(player)
local God = Instance.new("BoolValue",player)
God.Name = "GodMode"
God.Value = false
game.ReplicatedStorage.GodMode.OnServerEvent:Connect(function(user)
if user == player then
God.Value = not God.Value
end
end)
end)
For the script that you have above, the for loop will just need to be adjusted:
for _, KillBrick in pairs(KillBricksFolder:GetChildren()) do
KillBrick.Touched:Connect(function(touchPart)
local humanoid = touchPart.Parent:FindFirstChild("Humanoid")
if humanoid then
local plr = game.Players:GetPlayerFromCharacter(touchPart.Parent)
if not plr.GodMode.Value then
humanoid.Health = 0
end
end
end)
end
you should be using game.Players:GetPlayerFromCharacter as it’s faster and more reliable.
you could have a value in workspace be set to true if they have the gamepass and check if the value is true before setting their health to 0.
How can I get the TextButton to prompt the player to buy the gamepass if they don’t own it? Can I just use Market:PromptGamePassPurchase(Player, Gamepass ID)???
According to my ‘knowledge’. I might help you solve the code.
local KillBricksFolder = game.Workspace.KillParts
local EasyModeButton = game.StarterGui.EasyMode.EasyModeButton
for _, KillBrick in pairs(KillBricksFolder:GetChildren()) do
KillBrick.Touched:Connect(function(touchPart)
local player = game.Players:GetPlayerFromCharacter(touchPart.Parent)
if player then
local ownGamepass
local success, err = pcall(function()
ownGamepass = game:GetServices("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, YourGamepassIdHere)
end)
if ownGamepass then
local humanoid = player.Character:WaitForChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
end)
I’m not sure if it will work but if you see any errors, please provide the error code from the output.
I’m trying to make it to where if the TextButton Text is something specific then the humanoid.Health = 100 instead of 0 because if the god mode is enabled then we don’t want to kill the player. If the player owns the pass then they can press the button to enable god mode but if they don’t own it then the button will prompt you to buy the gamepass.
I know… I just need the kill parts to not kill the player if the god mode is on. you need to buy the god mode gamepass to toggle it. You need to press a TextButton to toggle it…