Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do use multiple functions for gamepass?

Code:

local player = game.Players.LocalPlayer
local char = player.Character
local GPS = game:GetService("MarketplaceService")
game:GetService("Players").PlayerAdded:Connect(function(plr)
    if GPS:UserOwnsGamePassAsync(plr.UserId,7853571) then
		local Part = script.Parent:WaitForChild("HumanoidRootPart")
		script.Light.Parent = Part
		Part.Light.Brightness = 1.5
		Part.Light.Range = 9
		wait(0.1)
		script:Destroy()
	else
		local Part = script.Parent:WaitForChild("HumanoidRootPart")
		script.Light.Parent = Part
		Part.Light.Brightness = 1
		Part.Light.Range = 6
		wait(0.1)
		script:Destroy()
    end

    if GPS:UserOwnsGamePassAsync(plr.UserId,7854035) then
		char.Humanoid.MaxHealth = 125
		char.Humanoid.Health = 125
	else
		char.Humanoid.MaxHealth = 100
		char.Humanoid.Health = 100
	end
end)

Apparently, when a player owns both gamepass, it doesn't set their health to specific health I wanted, also it doesn't emit light in a player. I was wondering what I could have done wrong :C

1 answer

The reason why your code won't run for both if statements is because in your first if statement, you are deleting the script in which the code is being ran regardless of which state it comes to.

To fix this just remove the parts where you are deleting the script.

local player = game.Players.LocalPlayer
local char = player.Character
local GPS = game:GetService("MarketplaceService")
game:GetService("Players").PlayerAdded:Connect(function(plr)
    if GPS:UserOwnsGamePassAsync(plr.UserId,7853571) then
        local Part = script.Parent:WaitForChild("HumanoidRootPart")
        script.Light.Parent = Part
        Part.Light.Brightness = 1.5
        Part.Light.Range = 9
    else
        local Part = script.Parent:WaitForChild("HumanoidRootPart")
        script.Light.Parent = Part
        Part.Light.Brightness = 1
        Part.Light.Range = 6
    end

    if GPS:UserOwnsGamePassAsync(plr.UserId,7854035) then
        char.Humanoid.MaxHealth = 125
        char.Humanoid.Health = 125
    else
        char.Humanoid.MaxHealth = 100
        char.Humanoid.Health = 100
    end
end)

But you have another problem, it seems like this code is located inside the character as well that it is a localscript, you want this code to only be located in one place and running on the server. So put this code into a normal script and put this code in the ServerScriptService, after this we need to fix the code to reflect this including changes to how light is put into the HumanoidRootPart

make sure to put the light instance inside the script

local light=script.Light --light path

local GPS = game:GetService("MarketplaceService")
game:GetService("Players").PlayerAdded:Connect(function(plr)
    local char=plr.Character
    
    if GPS:UserOwnsGamePassAsync(plr.UserId,7853571) then
        local Part = char:WaitForChild("HumanoidRootPart")
        local l=light:Clone()
        l.Brightness = 1.5
        l.Range = 9
	l.Parent=Part
    else
        local Part = char:WaitForChild("HumanoidRootPart")
	local l=light:Clone()
        l.Brightness = 1
        l.Range = 6
	l.Parent=Part
    end

    if GPS:UserOwnsGamePassAsync(plr.UserId,7854035) then
        char.Humanoid.MaxHealth = 125
        char.Humanoid.Health = 125
    else
        char.Humanoid.MaxHealth = 100
        char.Humanoid.Health = 100
    end
end)

1 comment

0

I've done as to what you have instructed. Now all is left is to fix the error output "ServerScriptService.GPS Check:8: attempt to index local 'char' (a nil value)". However, when I put them in StarterCharacterScript, the error was gone. But, I'll stick with ServerScriptService.

This discussion is preserved as it was. New answers, comments, and votes are closed.