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

RemoteEvent isn't connecting to a function when function has a sub-function?

Asked by
DemGame 271 Moderation Voter
3 years ago

Here is my code:

local bullet = game.ReplicatedStorage["Cyborg Assets"].bullet
local head = script.Parent.Parent:FindFirstChild("Head")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = Instance.new("RemoteEvent", ReplicatedStorage)
event.Name = "Cyborg SP1"

local function shootbullet()
    local debounce = false
    local Fireball = bullet:Clone()
    print("remoteEvent recieved")
    Fireball.BrickColor = BrickColor.new("Institutional white")
    Fireball.Transparency = 0
    Fireball.TopSurface = 0
    Fireball.BottomSurface = 0
    Fireball.CanCollide = false
    Fireball.Locked = true
    Fireball.CFrame = head.CFrame*CFrame.new(0,0,-10)
    local BodyVelocity = Instance.new("BodyVelocity")
    BodyVelocity.Parent = Fireball
    BodyVelocity.maxForce = Vector3.new(math.huge, math.huge, math.huge)
    BodyVelocity.velocity = head.CFrame.lookVector*40
local function Hurt(Part)
    local debounce = false
        if debounce == false then
        if Part.Parent:FindFirstChild("Humanoid") then
                Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health-25
                debounce = true
                wait(2)
                debounce = false
                wait()
            end
        end
        Fireball.Touched:connect(Hurt)
    wait()
    end
event.OnServerEvent:connect(shootbullet)
end

"remoteEvent received" is not appearing inside of the output but when I edit the code to this:

local bullet = game.ReplicatedStorage["Cyborg Assets"].bullet
local head = script.Parent.Parent:FindFirstChild("Head")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = Instance.new("RemoteEvent", ReplicatedStorage)
event.Name = "Cyborg SP1"

local function shootbullet()
    local debounce = false
    local Fireball = bullet:Clone()
    print("remoteEvent recieved")
    Fireball.BrickColor = BrickColor.new("Institutional white")
    Fireball.Transparency = 0
    Fireball.TopSurface = 0
    Fireball.BottomSurface = 0
    Fireball.CanCollide = false
    Fireball.Locked = true
    Fireball.CFrame = head.CFrame*CFrame.new(0,0,-10)
    local BodyVelocity = Instance.new("BodyVelocity")
    BodyVelocity.Parent = Fireball
    BodyVelocity.maxForce = Vector3.new(math.huge, math.huge, math.huge)
    BodyVelocity.velocity = head.CFrame.lookVector*40
end
event.OnServerEvent:connect(shootbullet)
local function Hurt(Part)
    local debounce = false
        if debounce == false then
        if Part.Parent:FindFirstChild("Humanoid") then
                Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health-25
                debounce = true
                wait(2)
                debounce = false
                wait()
            end
        end
        Fireball.Touched:connect(Hurt)
    wait()
end

Also, when I edit it to this way, the "Fireball" never appears.

1 answer

Log in to vote
1
Answered by
c_odez 20
3 years ago

In the first code block, "remoteEvent received" never prints because you're waiting for the event to be fired inside the function, as opposed to the second example where you do it outside. Having a function inside a function has nothing to do with it. The reason the fireball doesn't appear in the second example is that you aren't parenting the fireball to workspace. You also set the debounce to false whenever Hurt is called, so the debounce does nothing.

local bullet = game.ReplicatedStorage["Cyborg Assets"].bullet
local head = script.Parent.Parent:FindFirstChild("Head")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = Instance.new("RemoteEvent", ReplicatedStorage)
event.Name = "Cyborg SP1"

local function Hurt(Part)
        if debounce == false then
        if Part.Parent:FindFirstChild("Humanoid") then
                Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health-25
                debounce = true
                wait(2)
                debounce = false
            end
        end
end

local function shootbullet()
    local debounce = false
    local Fireball = bullet:Clone()
    print("remoteEvent recieved")
    Fireball.BrickColor = BrickColor.new("Institutional white")
    Fireball.Transparency = 0
    Fireball.TopSurface = 0
    Fireball.BottomSurface = 0
    Fireball.CanCollide = false
    Fireball.CFrame = head.CFrame*CFrame.new(0,0,-10)
    Fireball.Parent = game.Workspace
    local BodyVelocity = Instance.new("BodyVelocity")
    BodyVelocity.Parent = Fireball
    BodyVelocity.maxForce = Vector3.new(math.huge, math.huge, math.huge)
    BodyVelocity.velocity = head.CFrame.lookVector*40
    Fireball.Touched:connect(Hurt)
end
event.OnServerEvent:connect(shootbullet)

This should fix it.

0
Thanks. DemGame 271 — 3y
Ad

Answer this question