01 | function 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 |
07 | function 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(Vector 3. new(- 26 , 4.2 , - 878 )) |
12 | end |
13 | end |
14 | end |
15 | end |
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!
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:
01 | function 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(Vector 3. new(- 26 , 4.2 , - 878 )) |
11 | end |
12 | end |
13 | end |
14 | end |
15 | end |
16 |
17 | script.Parent.Touched:connect(OnTouched) |