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.
01function OnTouched(hit)
02    if game.Players:GetPlayerFromCharacter(hit.Parent) then
03        for _,Player in pairs(game.Players:GetPlayers()) do
04        if Player:FindFirstChild("leaderstats") then
05    if Player.leaderstats.Money.Value > 68000 then do
06    Player.leaderstats.Money.Value = Player.leaderstats.Money.Value -68000
07function OnTouch(hit)
08            if game.Players:GetPlayerFromCharacter(hit.Parent)then
09            Clone = game.Workspace["Nissan 240SX(Car)"]:Clone()
10            Clone.Parent = game.Workspace
11            Clone:MoveTo(Vector3.new(-26, 4.2, -878))
12            end
13            end
14            end
15            end
View all 23 lines...

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:

01function OnTouched(hit)
02    if hit.Parent:FindFirstChild("Humanoid") then -- it's a character
03        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
04        if player then -- it is also a player, not an npc
05            if player:FindFirstChild("leaderstats") then -- the player has statistics ;D
06                if player.leaderstats.Money.Value >= 68000 then
07                    player.leaderstats.Money.Value = player.leaderstats.Money.Value - 68000
08                    local Clone = game.Workspace["Nissan 240SX(Car)"]:Clone()
09                    Clone.Parent = game.Workspace
10                    Clone:MoveTo(Vector3.new(-26, 4.2, -878))
11                end
12            end
13        end
14    end
15end
16 
17script.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