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

+Value and :Destroy() won't work?(RE)

Asked by 5 years ago
-- Script
    game.Players.PlayerAdded:Connect(function(player)
        script.Parent.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                local lead = player:WaitForChild("leaderstats")
                local food1 = lead:WaitForChild("Food Collected")
                food1.Value = food1.Value + 2
                script.Parent:Destroy()
            end
        end)
    end)

if the food has been touched, it will +2 the value in leaderstats.Food Collected and Destroy() it, but it does it, but I touched about 30 of them, but only one has been destroyed and got +2 value in leaderstats. Can somebody enhance the script so that you can touch and collect(Destroy) it easily?

1 answer

Log in to vote
0
Answered by 5 years ago

You need to use :GetPlayerFromCharacter

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
    if Players:GetPlayerFromCharacter(hit.Parent) then
        local Player = Players[hit.Parent.Name]
        local leaderstats = Player:WaitForChild("leaderstats")

        local Food1 = leaderstats:WaitForChild("Food Collected")
        Food1.Vaalue = Food1.Value +2

        script.Parent:Destroy()
    end
end)

Thank me later

Ad

Answer this question