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

I am trying to make something happen when you kill an enemy zombie?

Asked by 5 years ago

I am trying to make a game that has ending to it, in which one ending you can kill the enemy and a text box pops up on your screen and you get kicked. I just cant figure out why this wont work (I am pretty new to scripting)

    local Zom = script.Parent:WaitForChild("Zombie")
    Zom.Died.Connect(function()
    print("test")
    script.Parent.Touched:connect(function(hit)
    game.Players.PlayerAdded:Connect(function(plr)
    plr.PlayerGui.Black.Frame.Active = true
plr.PlayerGui.Black.Frame.BackgroundTransparency = .9
wait(.1)
plr.PlayerGui.Black.Frame.BackgroundTransparency = .8
wait(.1)
plr.PlayerGui.Black.Frame.BackgroundTransparency = .7
wait(.1)
plr.PlayerGui.Black.Frame.BackgroundTransparency = .6
wait(.1)
plr.PlayerGui.Black.Frame.BackgroundTransparency = .5
wait(.1)
plr.PlayerGui.Black.Frame.BackgroundTransparency = .4
wait(.1)
plr.PlayerGui.Black.Frame.BackgroundTransparency = .3
wait(.1)
plr.PlayerGui.Black.Frame.BackgroundTransparency = .2
wait(.1)
plr.PlayerGui.Black.Frame.BackgroundTransparency = .1
wait(.1)
plr.PlayerGui.Black.Frame.BackgroundTransparency = 0
wait(2)
plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = .9
wait(.1)
plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = .8
wait(.1)
plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = .7
wait(.1)
plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = .6
wait(.1)
plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = .5
wait(.1)
plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = .4
wait(.1)
plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = .3
wait(.1)
plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = .2
wait(.1)
plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = .1
wait(.1)
plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = 0
wait(5)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player:Kick("Self Defence")

end) end) end)

0
You are connecting events inside of events in other words they will be connected when the event runs. You should connect the events outide of each other. User#5423 17 — 5y
0
Also you should be using loops / tweens for the gui fade User#5423 17 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

So, in Roblox, if you write part.Touched:Connect(function(hit), everything that touches the part will be named "hit" including the basepart. a way to verify if the parent of "hit" is a player is to:

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        > YOUR CODE HERE.
    end
end)

What I did is that after something touched, if his parent has a child name "Humanoid" then it's a player's character cuz players's characters always have a child name "Humanoid".

Also, at line 5, you wrote game.Players.PlayerAdded:Connect(function(plr), and this will find the player, but this line of code is envlopped by the touched function and we don't know when the player will touch the part, but it's sure that the player touches it AFTER he has joined. So that line of code "game.Players.PlayerAdded:Connect(function(plr)" will NEVER run. You can find the player's name by getting the "hit" 's parent's name AFTER verifying it's a player. Then you use that name and get it in game.Players cuz the player's character's name is the same as the player's name in game.Players. You can do this:

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        local plr = game.Players:WaitForChild(hit.Parent.Name)
    end
end)

Also, at line 6, you wrote that the GUI named "Black" will be active after the player touches the part. But the activity of a Gui doesn't change its visibility. Instead you can set the "Visible" in the Gui named "Black" already false , then when the player touches it you make it to true:

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        local plr = game.Players:WaitForChild(hit.Parent.Name)
        plr.PlayerGui.Black.Frame.Visible = true
    end
end)

Also, you can simplify the transparency effect of the gui with loops:

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        local plr = game.Players:WaitForChild(hit.Parent.Name)
        plr.PlayerGui.Black.Frame.Visible = true
        for i = 0, 1, 0.1 do
            plr.PlayerGui.Black.Frame.BackgroundTransparency = i
            wait(0.1)
        end
        wait(2)
        for i = 0, 1, 0.1 do
            plr.PlayerGui.Black.Frame.TextLabel.TextTransparency = i
            wait(0.1)
        end
    end
end)

Hope this helped!

Ad

Answer this question