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

Why is my remote event script having trouble with "clone" and "humanoid"??

Asked by 4 years ago

So I'm trying to make a fireball script and it works except only I can see it so I tried using a remote event. But the script in server script service is having problems with clone and humanoid. Is there anything I'm missing about remote events?? LocalScript in StarterPack:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local char = plr.Character or plr.CharacterAdded:wait()
local fireball = game.Lighting.FireBending.Fire1
local canattack = true
local checkdeath = true

mouse.KeyDown:connect(function(key)
    if key == "z" and canattack == true then
        game.ReplicatedStorage.RemoteEvent:FireServer()
    end
end)

while checkdeath == true do
    wait(.1)
    if plr.Character.Humanoid.Health <= 0 then
        script:remove()
    end
end

Script in ServerScriptService

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)
    canattack = false

    local fire = fireball:Clone()
    fire.Parent = workspace
    fire.CFrame = char.Humanoid.Torso.CFrame * CFrame.new(0,0,-10)

    local bodyv = Instance.new("BodyVelocity")
    bodyv.MaxForce = Vector3.new(1e8,1e8,1e8)
    bodyv.Velocity = char.Humanoid.Torso.CFrame.lookVector * 150
    bodyv.Parent = fire

    local fireanim1 = char.Humanoid:LoadAnimation(script.FireAnim1)
    local fireanim2 = char.Humanoid:LoadAnimation(script.FireAnim2)
    local fireanim3 = char.Humanoid:LoadAnimation(script.FireAnim3)
    local choose = math.random(1,3)
    if choose == 1 then
        fireanim1:Play()
    elseif choose == 2 then
        fireanim2:Play()
    elseif choose == 3 then
        fireanim3:Play()
    end


    wait(2)
    fire:remove()
    wait(1)
    canattack = true
end)

1 answer

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

Hello. The problem is that you did not index "char" and "fireball" as a variable so it would return nil and the output would say something like Attempt to index nil with Humanoid. Change your event handler script to this:

local fireball = game:GetService("Lighting").FireBending.Fire1

game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(plr)\
    local char = player.Character

    canattack = false

    local fire = fireball:Clone()
    fire.Parent = workspace
    fire.CFrame = char.Humanoid.Torso.CFrame * CFrame.new(0,0,-10)

    local bodyv = Instance.new("BodyVelocity")
    bodyv.MaxForce = Vector3.new(1e8,1e8,1e8)
    bodyv.Velocity = char.Humanoid.Torso.CFrame.lookVector * 150
    bodyv.Parent = fire

    local fireanim1 = char.Humanoid:LoadAnimation(script.FireAnim1)
    local fireanim2 = char.Humanoid:LoadAnimation(script.FireAnim2)
    local fireanim3 = char.Humanoid:LoadAnimation(script.FireAnim3)
    local choose = math.random(1,3)
    if choose == 1 then
        fireanim1:Play()
    elseif choose == 2 then
        fireanim2:Play()
    elseif choose == 3 then
        fireanim3:Play()
    end


    wait(2)
    fire:Destroy()
    wait(1)
    canattack = true
end)

Also, don't put your LocalScript into starter pack. Place it in StarterPlayer > StarterCharacterScripts. Also, use UserInputService as Mouse.KeyDown is deprecated. Change your LocalScript to this:

local UserInputService = game:GetService("UserInputService")
local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local char = plr.Character or plr.CharacterAdded:wait()
local fireball = game:GetService("Lighting"):WaitForCHild("FireBending").Fire1
local canattack = true
local checkdeath = true

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.Z and canattack == true and not gameProcessedEvent then
        game:GetService("ReplicatedStorage").RemoteEvent:FireServer()
    end
end)

--[[while checkdeath == true do
    wait(0.1)
    if plr.Character.Humanoid.Health <= 0 then
        script:remove()
    end
end--]]

I used :WaitForChild() to prevent the script from erroring if the fireball hasn't loaded yet. Also, there's no reason to remove the script if checkdeath == true, so I commented it out.

Also, I used :GetService() on the variables as it checks if the service is existent. If it isn't then it creates the service. It's useful for things like UserInputService where you can't just use the conditional dot operator. Please upvote and accept this answer if it helped.

Ad

Answer this question