Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is wrong with this script? (Please Answer)

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

✓ Accepted 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:

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)

2 comments

0

Thanks! :)

0

Found out the problem. You put "humanoid" and not "Humanoid". No big deal though. Thank you so much! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.