Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Please fix this GamePass Script?

Asked by 10 years ago

In my game, I am making a mailbox where if you have a gamepass and you touch it, your money increases by the amount of money in the model's value. But it doesn't work and I don't know why. Can you help?

Here's what I need fixed:

--------------------
--| WaitForChild |--
--------------------

-- Waits for parent.child to exist, then returns it
local function WaitForChild(parent, childName)
    assert(parent, "ERROR: WaitForChild: parent is nil")
    while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
    return parent[childName]
end

-----------------
--| Variables |--
-----------------

local GamePassService = Game:GetService('GamePassService')
local PlayersService = Game:GetService('Players')

local VipDoor = script.Parent

local GamePassIdObject = WaitForChild(script, 'GamePassId')

local JustTouched = {}

local MoneyBoard = Script.Parent.Parent.Money

-----------------
--| Functions |--
-----------------
local function Collect(character, hitPart)
    local players = game.Players:children() 
    local human = hit.Parent:findFirstChild("Humanoid") 
    local player = getPlayer(human) 
    local stats = player:findFirstChild("leaderstats") 
    local sp = stats:findFirstChild("Money") 
    sp.Value = Sp.Value + MoneyBoard.Value
    MoneyBoard.Value = 0
end

-- When a player with the game pass touches the door, teleport them to the other side
local function OnTouched(otherPart)
    if otherPart and otherPart.Parent and otherPart.Parent:FindFirstChild('Humanoid') then
        local player = PlayersService:GetPlayerFromCharacter(otherPart.Parent)
        if player and not JustTouched[player] then
            JustTouched[player] = time()
            if GamePassService:PlayerHasPass(player, GamePassIdObject.Value) then
                Collect(player.Character, otherPart)
            end
        end
    end
end

-- Removes old entries in JustTouched
local function RemoveOldTouches()
    for player, touchTime in pairs(JustTouched) do
        if time() > touchTime + 0.3 then
            JustTouched[player] = nil
        end
    end
end

--------------------
--| Script Logic |--
--------------------

VipDoor.Touched:connect(OnTouched)

while true do
    RemoveOldTouches()
    wait(1/30)
end

2 answers

Log in to vote
0
Answered by
hiccup111 231 Moderation Voter
10 years ago

line 31, I think 'children()' is deprecated, try :GetChildren()

line 33, is there a function defined as 'getPlayer'?

line 41, you may have better chance getting the player doing this:

if game.Players:FindFirstChild(otherPart.Parent.Name) then

Also, I didn't know functions could be local? Even so, what's the point?

I can't see anything else that may pose a problem.

Ad
Log in to vote
0
Answered by 4 years ago

--| WaitForChild |--

-- Waits for parent.child to exist, then returns it local function WaitForChild(parent, childName) assert(parent, "ERROR: WaitForChild: parent is nil") while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end return parent[childName] end


--| Variables |--

local GamePassService = game:GetService('MarketplaceService') local PlayersService = game:GetService('Players')

local VipDoor = script.Parent

local GamePassIdObject = script:WaitForChild( 'GamePassId')

local JustTouched = {}


--| Functions |--

-- Finds out which side the player is on and teleports them to the other local function TeleportToOtherSide(character, hitPart) local bottomOfDoor = VipDoor.CFrame.p - Vector3.new(0, VipDoor.Size.Y / 2, 0) local inFrontOfDoor = bottomOfDoor + VipDoor.CFrame.lookVector * 3 local behindDoor = bottomOfDoor - VipDoor.CFrame.lookVector * 3

local distanceToFront = (inFrontOfDoor - hitPart.Position).magnitude
local distanceToBack = (behindDoor - hitPart.Position).magnitude
if distanceToFront < distanceToBack then
    character:MoveTo(behindDoor)
else
    character:MoveTo(inFrontOfDoor)
end

end

-- When a player with the game pass touches the door, teleport them to the other side local function OnTouched(otherPart) if otherPart and otherPart.Parent and otherPart.Parent:FindFirstChild('Humanoid') then local player = PlayersService:GetPlayerFromCharacter(otherPart.Parent) if player and not JustTouched[player] then JustTouched[player] = time() if GamePassService:UserOwnsGamePassAsync(player.userId, GamePassIdObject.Value) then TeleportToOtherSide(player.Character, otherPart) end end end end

-- Removes old entries in JustTouched local function RemoveOldTouches() for player, touchTime in pairs(JustTouched) do if time() > touchTime + 0.3 then JustTouched[player] = nil end end end


--| Script Logic |--

VipDoor.Touched:connect(OnTouched)

while true do RemoveOldTouches() wait(1/30) end

Answer this question