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

Why do the meshes teleport to the last player who activated the script?

Asked by 4 years ago

I made a script where when you press R you do a punch animation and release a shockwave, it works fine when playing alone but when multiple players use the script during the few seconds it's active then the shockwave's mesh teleports to the player who used it last even though it's supposed to clone a separate one from serverstorage.

When I tried using Instance.new("Part") instead of cloning in a mesh from serverstorage it worked fine.

The script:


local ReplicatedStorage = game:GetService("ReplicatedStorage") local Table = {} ReplicatedStorage.RemoteEvent3.OnServerEvent:Connect(function(player) local function ShockWave() wait(0.6) game:GetService("ServerStorage").ShockWaveMesh:Clone().Parent = game.Workspace local ShockWave = game.Workspace.ShockWaveMesh ShockWave.Position = player.Character.RightHand.Position ShockWave.Orientation = player.Character.HumanoidRootPart.Orientation ShockWave.Size = Vector3.new(1,1,0.5) ShockWave.Transparency = 0.5 ShockWave.CanCollide = false game:GetService("ServerStorage").ShockWaveMesh2:Clone().Parent = game.Workspace local BodyVelocity = Instance.new("BodyVelocity") local ShockWave2 = game.Workspace.ShockWaveMesh2 BodyVelocity.Parent = ShockWave2 ShockWave2.Orientation = ShockWave.Orientation ShockWave2.Position = ShockWave.Position ShockWave2.Size = Vector3.new(5,5,5) ShockWave2.Transparency = 0.5 ShockWave2.CanCollide = false BodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge) BodyVelocity.Velocity = ShockWave.CFrame.lookVector * 40 ShockWave2.Touched:Connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid and hit.Parent.Name ~= player.Name then local found = false for i = 1,#Table do if Table[i] == hit.Parent.Name then found = true end end if found == false then table.insert(Table,game.Players:GetPlayerFromCharacter(hit.Parent).Name) humanoid:TakeDamage(30) found = true wait(3) for i = 1,#Table do if Table[i] == hit.Parent.Name then table.remove(Table,i) end end end end end) spawn(function() for i = 1,100 do wait() ShockWave.Size = ShockWave.Size + Vector3.new(1,1,0.01) ShockWave.Transparency = ShockWave.Transparency + 0.005 ShockWave2.Size = ShockWave2.Size + Vector3.new(1,1,1) ShockWave2.Transparency = ShockWave2.Transparency + 0.005 if ShockWave.Transparency == 1 or i == 100 or ShockWave2.Transparency == 1 then ShockWave:Destroy() ShockWave2:Destroy() end end end) spawn(function() for i = 1,100 do wait() ShockWave2.Orientation = ShockWave2.Orientation + Vector3.new(0,0,3) ShockWave2.Position = ShockWave2.Position + Vector3.new(0,0.1,0) end end) end ShockWave() end)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The reason that the shockwave teleports to the other player is because the script has no way of knowing which shockwave you want to re-position. For example, if there were 100 shockwaves happening at once in the server, then typing...

game.Workspace.Shockwave.Position = (Insert Vector3 Here)

...will not tell the script what shockwave you want exactly and will automatically choose the newest instance.

To fix this problem, instead of typing...

    game:GetService("ServerStorage").ShockWaveMesh:Clone().Parent = game.Workspace
    local ShockWave = game.Workspace.ShockWaveMesh
    ShockWave.Position = player.Character.RightHand.Position
    ShockWave.Orientation = player.Character.HumanoidRootPart.Orientation
    ShockWave.Size = Vector3.new(1,1,0.5)
    ShockWave.Transparency = 0.5
    ShockWave.CanCollide = false

...you would want to save that instance of the shockwave as a variable. This is how I would rewrite it (without changing too much of the script):

   local ShockWave = game:GetService("ServerStorage").ShockWaveMesh:Clone()
     ShockWave.Parent = workspace;

    ShockWave.Position = player.Character.RightHand.Position;
    ShockWave.Orientation = player.Character.HumanoidRootPart.Orientation;
    ShockWave.Size = Vector3.new(1,1,0.5);
    ShockWave.Transparency = 0.5;
    ShockWave.CanCollide = false;

Do this for both ShockWave and Shockwave2 and it should be fixed. Here is the final code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Table = {}

ReplicatedStorage.RemoteEvent3.OnServerEvent:Connect(function(player)

local function ShockWave()
    wait(0.6)

    local ShockWave = game:GetService("ServerStorage").ShockWaveMesh:Clone()
    ShockWave.Parent = workspace;

    ShockWave.Position = player.Character.RightHand.Position
    ShockWave.Orientation = player.Character.HumanoidRootPart.Orientation
    ShockWave.Size = Vector3.new(1,1,0.5)
    ShockWave.Transparency = 0.5
    ShockWave.CanCollide = false

    local BodyVelocity = Instance.new("BodyVelocity")
    local ShockWave2 = game:GetService("ServerStorage").ShockWaveMesh:Clone()
    ShockWave2.Parent = workspace;

    BodyVelocity.Parent = ShockWave2
    ShockWave2.Orientation = ShockWave.Orientation
    ShockWave2.Position = ShockWave.Position
    ShockWave2.Size = Vector3.new(5,5,5)
    ShockWave2.Transparency = 0.5
    ShockWave2.CanCollide = false
    BodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    BodyVelocity.Velocity = ShockWave.CFrame.lookVector * 40
    ShockWave2.Touched:Connect(function(hit)
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid and hit.Parent.Name ~= player.Name then
            local found = false
            for i = 1,#Table do
                if Table[i] == hit.Parent.Name then
                found = true
            end
            end
        if found == false then
            table.insert(Table,game.Players:GetPlayerFromCharacter(hit.Parent).Name)
            humanoid:TakeDamage(30)
            found = true
            wait(3)
            for i = 1,#Table do
                if Table[i] == hit.Parent.Name then
                    table.remove(Table,i)


                end
            end
        end
    end
end)


    spawn(function()
    for i = 1,100 do    
        wait()
        ShockWave.Size = ShockWave.Size + Vector3.new(1,1,0.01)
        ShockWave.Transparency = ShockWave.Transparency + 0.005
        ShockWave2.Size = ShockWave2.Size + Vector3.new(1,1,1)
        ShockWave2.Transparency = ShockWave2.Transparency + 0.005
        if ShockWave.Transparency == 1 or i == 100 or ShockWave2.Transparency == 1 then
            ShockWave:Destroy()
            ShockWave2:Destroy()

        end
    end
end)
    spawn(function()
for i = 1,100 do
wait()
ShockWave2.Orientation = ShockWave2.Orientation + Vector3.new(0,0,3)
ShockWave2.Position = ShockWave2.Position + Vector3.new(0,0.1,0)
end
end)    




end


ShockWave()
end)

I hope this helped :)

0
I don't really understand how giving it a variable fixes it since if you activate the script twice they should have the same variable, but this worked so thank you Guest_NumberNotFound 19 — 4y
Ad

Answer this question