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

What is wrong with this script? (Please Answer)

Asked by 10 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.
function OnTouched(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
        for _,Player in pairs(game.Players:GetPlayers()) do
        if Player:FindFirstChild("leaderstats") then
    if Player.leaderstats.Money.Value > 68000 then do
    Player.leaderstats.Money.Value = Player.leaderstats.Money.Value -68000
function OnTouch(hit)
            if game.Players:GetPlayerFromCharacter(hit.Parent)then
            Clone = game.Workspace["Nissan 240SX(Car)"]:Clone()
            Clone.Parent = game.Workspace
            Clone:MoveTo(Vector3.new(-26, 4.2, -878))
            end
            end
            end
            end
        end
        end
    end
end


script.Parent.Touched:connect(OnTouch)
script.Parent.Touched:connect(OnTouched)

What is wrong with this script? I'm trying to have the players buy the car and then the car is suppose to spawn at its coo-ordinates.

Problems:

1) The car doesn't spawn at its coordinates.

2) It not only takes $68000 from the player, but from everyone else's money too.

Any Help? Please Answer!

1 answer

Log in to vote
3
Answered by 10 years ago
Edited 8 years ago

Okay, when the script's parent is touched two functions execute: OnTouched and Ontouch.

OnTouched: Checks if what hit is a player, if so it checks if the player has leaderstats, if so it takes 68000 from the player's money.

OnTouch: Checks if what hit is a player, if so it spawns the car.

Instead of two functions we need one function that checks if what hit is a player (I always check for character first, just a preference thingy) and if that is true we check for leaderstats and if we have them we take 68000 from the player's money and spawn in the car. That would look like this:

function OnTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- it's a character
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then -- it is also a player, not an npc
            if player:FindFirstChild("leaderstats") then -- the player has statistics ;D
                if player.leaderstats.Money.Value >= 68000 then
                    player.leaderstats.Money.Value = player.leaderstats.Money.Value - 68000
                    local Clone = game.Workspace["Nissan 240SX(Car)"]:Clone()
                    Clone.Parent = game.Workspace
                    Clone:MoveTo(Vector3.new(-26, 4.2, -878))
                end
            end
        end
    end
end

script.Parent.Touched:connect(OnTouched)
0
Thanks! :) notes4u99 65 — 10y
0
Found out the problem. You put "humanoid" and not "Humanoid". No big deal though. Thank you so much! :) notes4u99 65 — 10y
Ad

Answer this question