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

How to make a "Part"(Brick) spawn a car in front of you when you touch it?

I want to make a code that when the brick is touched, a car appears infront of the players character, but right now my code isnt working. May you please help me? Also, if my code is bad sorry its because I started scripting 5 days ago... :/

Code:

local crvpart = game.Workspace.CRV
local v = game.ServerStorage["CR-V"]
local character = game.Players.LocalPlayer.Character


crvpart.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then 
		local vly = v:Clone()
			vly.Parent = game.Workspace
			vly:MoveTo(character.UpperTorso.Position + Vector3.new(0,0,10))
	end
end)

1 comment

0

Are you using LocalScript? LocalPlayer can only work through LocalScript.

1 answer

If you want to do it without LocalScript then

function PlayerAdded (Player) -- declare PlayerAdded as a function and Player as variable (in this case is the player)
    local crvpart = game.Workspace.CRV -- define variable crvpart as part CRV
    local v = game.ServerStorage["CR-V"] -- define v as stuff CR-V
    local character = Player.Character -- define character as the player (if testing mode, Player1)


    crvpart.Touched:connect(function(hit) -- function run when something touch it.
        if hit.Parent:FindFirstChild("Humanoid") then  -- if player has a humanoid
            local vly = v:Clone() -- clone v
            vly.Parent = game.Workspace --- define cloned item parent as workspace
            vly:MoveTo(character.UpperTorso.Position + Vector3.new(0,0,10)) -- move to the desired location
        end
    end)
end
game.Players.PlayerAdded:connect (PlayerAdded) -- Function run when a player entered

DISCLAIMER: As I type this from from my phone, I have not test it yet.

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