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

Script only works once?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

I have a localScript in StarterGui that activates some code once a value is set to true. The problem is, in a server, the code only works once (before you die). Once you die, it spawns the map but stops at line 208. Here's the full code.

script.B:Play()
wait(1)
dif=5
function reload()
    cl=game.Players.LocalPlayer.Character:GetChildren()

    for a=1,#cl do
        if cl[a]:IsA("BasePart") then
    cl[a].CFrame=CFrame.new(game.Workspace.SpawnLocation.Position+Vector3.new(0,5,0))
        end
        end
    c=game.Workspace.CurrentCamera:GetChildren()
        for f=1,#c do
            c[f]:Destroy()
        end
dr=false

m=math.random(1,4)
print (m)
if m==1 then
    dx=30
    dy=30

elseif m==2 then

    dx=5
    dy=5

elseif m==3 then
    dx=5
    dy=30
elseif m==4 then
    dx=30
    dy=5
end
for i=1,100 do

sx=math.random(2,34)
sz=math.random(2,34)
s1x=15
s2x=14
s1y=34
s2y=34
sp=false
end


for i=1,35 do

    for j=1,35 do
pl=false        

        --Shop's first tile

        if i==s1x and j==s1y and not (i==1 or i==35 or j==1 or j==35) then
            p=  game.Workspace.SHOP1:Clone()
        p.Parent=game.Workspace.CurrentCamera
        p:MoveTo(Vector3.new(i*20,25,j*20))
        pl=true
        end

                --Shop's second tile

        if i==s2x and j==s2y and not (i==1 or i==35 or j==1 or j==35) then
            p=  game.Workspace.SHOP2:Clone()
        p.Parent=game.Workspace.CurrentCamera
        p:MoveTo(Vector3.new(i*20,25,j*20))
        pl=true
        end

        --Door
                if not dr and i==dx and j==dy and not (i==1 or i==35 or j==1 or j==35) then
            p=  game.Workspace.Door:Clone()
            print("Door")
        p.Parent=game.Workspace.CurrentCamera

                p.Enter.Touched:connect(function(hit)

            if hit.Parent:findFirstChild("Humanoid") then
                hit.Parent.Torso.Anchored=true
                local gui=script.Parent.Intermission
game.Players.LocalPlayer.Floor.Value=game.Players.LocalPlayer.Floor.Value+1
                repeat wait()
                    gui.Fade.Tex.TextTransparency=gui.Fade.Tex.TextTransparency-0.02
                    gui.Fade.TextTransparency=gui.Fade.TextTransparency-0.02
                    gui.Fade.BackgroundTransparency=gui.Fade.TextTransparency-0.02
                until gui.Fade.TextTransparency<=0

                gui.Fade.Tex.TextTransparency=0
                gui.Fade.BackgroundTransparency=0
        c=game.Workspace.CurrentCamera:GetChildren()
        reload()


            end
            end)
        p:MoveTo(Vector3.new(i*20,25,j*20))
        pl=true
        dr=true
            end

    --Wall
        if math.random(0,math.floor(50/(dif)))==2 and not pl and not (i==1 or i==35 or j==1 or j==35) then
        p=  game.Workspace.Wall:Clone()
        p.Parent=game.Workspace.CurrentCamera
        p:MoveTo(Vector3.new(i*20,25,j*20))
        pl=true
        end

        --Decor
        if math.random(0,math.floor(100/dif))==2 and not pl and not (i==1 or i==35 or j==1 or j==35) then
        p=  game.Workspace.Decor:Clone()
        p.Parent=game.Workspace.CurrentCamera
        p.Touch.Touched:connect(function(hit)
            hit.Parent.Torso.Velocity=Vector3.new(0,150,0)
        end)
        p:MoveTo(Vector3.new(i*20,25,j*20))
        pl=true
        end

                        if math.random(0,math.floor(100/dif))==2 and not pl and not (i==1 or i==35 or j==1 or j==35) then
        p=  game.Workspace.Bomb:Clone()
        p.Parent=game.Workspace.CurrentCamera

        p.Plate.Touched:connect(function(hit)
            np=p
            e=Instance.new("Explosion",game.Workspace.CurrentCamera)
        e.Position=game.Players.LocalPlayer.Character.Torso.Position    
            np:Destroy()

        end)
        p:MoveTo(Vector3.new(i*20,25,j*20))
        pl=true
                        end

    --Lava  
                if math.random(0,math.floor(100/dif))==2 and not pl and not (i==1 or i==35 or j==1 or j==35) then
        p=  game.Workspace.Lava:Clone()
        p.Parent=game.Workspace.CurrentCamera
        p.Hurt.Touched:connect(function(hit)
            if hit.Parent:findFirstChild("Humanoid") then
            hit.Parent.Humanoid:TakeDamage(100)
            end
        end)
        p:MoveTo(Vector3.new(i*20,25,j*20))
        pl=true
                end

--Spawn     
                        if not sp and i==sx and j==sz and not (i==1 or i==35 or j==1 or j==35) then
            p=  game.Workspace.Spawn:Clone()


                    --game.Players.LocalPlayer.Character.Torso.CFrame=p.SpawnLocation.CFrame
        p.Parent=game.Workspace.CurrentCamera
        p:MoveTo(Vector3.new(i*20,25,j*20))
        pl=true

                        end

    --Ground                        
        if not pl and not (i==1 or i==35 or j==1 or j==35) then
        --  for s=1,math.random(1,3) do
            p=  game.Workspace.Ground:Clone()
        p.Parent=game.Workspace.CurrentCamera
        p:MoveTo(Vector3.new(i*20,25,j*20))
    --  end
        pl=true


        end

        if i==1 or i==35 or j==1 or j==35 then
    p=  game.Workspace.BigWall:Clone()
        p.Parent=game.Workspace.CurrentCamera
        p:MoveTo(Vector3.new(i*20,25,j*20))

        end

    end

end

r=game.Workspace.Roof:Clone()
r.Parent=game.Workspace.CurrentCamera
r:MoveTo(Vector3.new(360, 26, 380))
    game.Players.LocalPlayer.Character.Parent=game.Workspace
end

reload()
wait(.1)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed=30

game.Players.LocalPlayer.Character.Humanoid.Died:connect(function()
game.Players.LocalPlayer.InMine.Value=false
game.Players.LocalPlayer.Floor.Value=0
dead=true
for i=1,100 do
    wait()
    script.Sound.Pitch=script.Sound.Pitch-0.01
end
--reload()
end)


wait(1)

    repeat wait() until game.Players.LocalPlayer.InMine.Value==true
    print("In mine")
    game.Players.LocalPlayer.Floor.Value=1


    dead=false
    gui=script.Parent.Intermission  

for i=1,100 do
    script.B.Volume=script.B.Volume-0.01
end
script.B:Stop()
repeat wait()
                    gui.Fade.Tex.TextTransparency=gui.Fade.Tex.TextTransparency-0.02
                    gui.Fade.TextTransparency=gui.Fade.TextTransparency-0.02
                    gui.Fade.BackgroundTransparency=gui.Fade.TextTransparency-0.02
                until gui.Fade.TextTransparency<=0

                wait(2)
                script.Sound:Play()
                repeat wait()
                    gui.Fade.Tex.TextTransparency=gui.Fade.Tex.TextTransparency+0.02
                    gui.Fade.TextTransparency=gui.Fade.TextTransparency+0.02
                    gui.Fade.BackgroundTransparency=gui.Fade.TextTransparency+0.02
                until gui.Fade.TextTransparency>=1




    while wait() do
        o=game.Players.LocalPlayer.Floor.Value
        repeat wait() until game.Players.LocalPlayer.Floor.Value~=o
        print("New floor!")
        wait(3)
            game.Players.LocalPlayer.Character:MoveTo(Vector3.new(360, 26, 380))
dif=dif+0.5
print(dif)
        repeat wait()
                    gui.Fade.Tex.TextTransparency=gui.Fade.Tex.TextTransparency+0.02
                    gui.Fade.TextTransparency=gui.Fade.TextTransparency+0.02
                    gui.Fade.BackgroundTransparency=gui.Fade.TextTransparency+0.02
        until gui.Fade.TextTransparency>=1

        game.Players.LocalPlayer.Character.Torso.Anchored=false
    end






Here's the code that sets the value to true.

script.Parent.Touched:connect(function(hit)
    if game.Players:findFirstChild(hit.Parent.Name) and not hit.Parent:findFirstChild("ForceField") then
f=Instance.new("ForceField",hit.Parent)
game:GetService("Debris"):AddItem(f,8)

        game.Players:findFirstChild(hit.Parent.Name).InMine.Value=true
wait(3)     
        hit.Parent:MoveTo(Vector3.new(360, 26, 380))    
    end
end)

It all seems to work in studio. I don't see the problem. Any help? Thanks in advance.

EDIT: I figured out the problem is in the second code. It only sets the value to true once. Still don't know why...

0
Who do expect to actually read through this and answer i, we are all extremely lazy koolkid8099 705 — 8y
0
Why are you posting comments 3 times? ChemicalHex 979 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

I am having the exact same issue with local scripts. Whenever the player dies, the script does not work anymore. The only thing I could think of to solve this is to create a master script to place the local scripts inside the character in the workspace. Here is one of my current scripts that places the local script into all of the players after death and when they join

function onPlayerAdded(player)
    print(player)
    print(player.Name.." has entered the game.")
    wait()
    local lava = ServerScript.LavaHit:Clone()
    local workspaceChild = workspace:GetChildren()
    for _, workspaceChild in pairs(workspaceChild) do
        if workspaceChild.ClassName == "Model" then
            local Child = workspaceChild:GetChildren()
            wait()
            for _, Child in pairs(Child) do
                if Child.ClassName == "Humanoid" then
                        lava.Parent = workspaceChild
                        lava.Disabled = false
                end
            end
        end
    end
end

function reAddScript(character)
    local lava = ServerScript.LavaHit:Clone()
    print(character)
    lava.Parent = workspace.character
    lava.Disabled = false
end

function onPlayerDeath(player)
    print(player.." has died")
    local x = script:FindFirstChild("Dead_"..player)
    x.Value = false
    reAddScript(workspace:WaitForChild(player))
end

while true do 
    players = playersService:GetPlayers()
    for _, CurrentPlayer in pairs (players) do
        print(CurrentPlayer)
        if script:WaitForChild("Dead_".. CurrentPlayer) then 
            local Value = script:WaitForChild("Dead_"..CurrentPlayer)
            if Value.Value == true then
                onPlayerDeath(CurrentPlayer)
            end
        end
    end
    wait()
end

However in my script there are two issues with this, I cannot Use CurrentPlayer and add it to a string and the script seems to only be placing it in one of the players thus far. But I think this may be able to solve your problem, at least if you can fix the issue. Hope this helped, good luck!

Ad

Answer this question