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

Can someone correct this code?

Asked by 10 years ago

Okay, so my code is supposed to award the player a player point, destroy the part, wait a minute, then put the part back in its spot. The problem is, it doesent do A SINGLE THING I told it to do. Plz help. Here's my code.

function eggCollect(hit)

    local db = false
    if not hit.Parent:FindFirstChild("Humanoid") or db then return end
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
    db = true
    PointsService:AwardPoints(player.userId, 1)
    db = false
    print 'Player points awarded'
    end

    script.Parent:Destroy()
    wait(60)
    script.Parent:Clone()

end

script.Parent.Touched:connect(onTouch)

ALSO my workspace looks like this:

^Workspace ^BasicEgg Mesh Script that I'm using

2 answers

Log in to vote
1
Answered by 10 years ago

There isn't anything wrong with your code. It's just that there are two functions, but you only have one. You need to make a function hit, and a function award points. Since you tried to combine two functions into one, it's not working.

`-- declare service
local PointsService = Game:GetService("PointsService")

-- Bind function to player added event
game.Players.PlayerAdded:connect(function(player)
    -- Get total number of points the game has available
    local pointsToAward = PointsService:GetAwardablePoints()
    -- Get total number of points this game has already awarded to the player
    local universeBalance = PointsService:GetGamePointBalance(player.userId)
    -- Check if the game has points to award and if the player hasn't gotten any points yet. 
    -- If both are true, then give the player a point.
    if ( pointsToAward > 0 and universeBalance == 0) then
        -- pcall here, as AwardPoints *will throw an error* if another server has awarded the
        -- points that were available between us checking how many were available and
        -- us actually awarding the points. There is currently no way to avoid this pcall,
        -- whenever you AwardPoints you should always wrap it in a pcall.
        pcall(function()
            PointsService:AwardPoints(player.userId, 1)
        end)
    end
end)

-- Bind function to when points are successfully awarded
PointsService.PointsAwarded:connect(function(userId, userBalanceinUni, userBalance)
    -- Show message indicating that a player has gotten points
    local message = Instance.new('Message', game.Workspace)
    message.Text = "Point awarded to " .. userId .. ". This player now has " .. userBalance .. " points total!"
    wait(5)
    message:Destroy()
end)`
Ad
Log in to vote
1
Answered by 10 years ago

May I point out

    script.Parent:Destroy()
    wait(60)
    script.Parent:Clone()

You need to make this an external script because you destroy the parent which destroys the children too. The script stops running from there

Answer this question