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

Whats the difference between these scripts?

Asked by 9 years ago

1.

script.Parent.Touched:connect(function()
    script.Parent:FindFirstChild("Humanoid") -- Checks for player
    local Scrip = script.Parent
    Instance.new("Explosion", game.Workspace.Part) -- Puts Explosion into a part
    local Explosion = game.Workspace.Part.Explosion
    game.Workspace.Part.Explosion.Position = Scrip.Position -- Gives explosion a Position
end)

Vs.

2.

script.Parent.Touched:connect(function()
    script.Parent:FindFirstChild("Humanoid") -- Checks for player
    local Explosion = Instance.new("Explosion",script.Parent) -- Explosion instance and location
    Explosion.Position = script.Parent.Position -- Gives Explosion a position
end)


Why does script 2 works better than Script 1? Script 1 explodes in two different places at the same time, the workspace at (0, 0, 0) and in the brick.

Script 2 Explodes in the brick only.

What's the difference? I've been reading this for a while, and both of them gives the explosion a Location, a Position, and even a Parent. What's wrong here?

1 answer

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

I'm not sure exactly what it could be, but Lua instructions take a millisecond or two to process, so the extra variable creation in the first is probably causing the issues.

Try this to see if it's any better:

script.Parent.Touched:connect(function()
    script.Parent:FindFirstChild("Humanoid") -- Checks for player
    local Explosion = Instance.new("Explosion", game.Workspace.Part) --Instance.new returns the created Instance
    Explosion.Position = script.Parent.Position --And you should probably reference the variable you just created. Also, creating a variable for something you use exactly once probably isn't a good idea.
end)
0
But, I used the Explosion variable only once and it's fine. james24dj 90 — 9y
Ad

Answer this question