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

RemoteEvent not working?

Asked by
tre821 25
8 years ago

Okay so, I'm trying to get a projectile to not do any damage to the person shooting it. At first I got it to work inside of studio, but it wouldn't work when playing. I was told to try using RemoteEvents, and I did. For some reason, the way I did it, I'm able to get the server script to return a local player's name, but it only works once. The rest of the time, the event returns nill. Could someone help me? What's in the local script:

local variable cooldownFS = 1
local mouse = game.Players.LocalPlayer:GetMouse()
function shootFS()
    if cooldownFS == 1 then
        print("Works")
        cooldownFS = 0
    local fireballs = Instance.new("Part")
    fireballs.TopSurface = "Smooth"
    fireballs.BottomSurface = "Smooth"
    fireballs.BrickColor = BrickColor.new("Really red")
    fireballs.Transparency = 0.3
    fireballs.Size = Vector3.new(2,2,2)
    fireballs.CFrame = game.Players.LocalPlayer.Character.Torso.CFrame *CFrame.new(0,0,-6)
    fireballs.Parent = game.Workspace
    fireballs.Anchored = true
    fireballs.CanCollide = true
    local damage = script.Damage:clone()
    damage.Parent = fireballs
    damage.Disabled = false
    local mesh = Instance.new("SpecialMesh", fireballs)
    mesh.MeshType = "Sphere"
    local fire = Instance.new("Fire", fireballs)
    fire.Heat = 8
    fire.Size = 20
    local re = Instance.new("RemoteEvent")
    re.Parent = game.Workspace
    re.Name = "re"
    re:FireServer(game.Players.LocalPlayer.Name)
    local bv2 = Instance.new("BodyVelocity")
    bv2.maxForce = Vector3.new(math.huge,math.huge,math.huge)
    bv2.velocity =  (mouse.Hit.p-game.Players.LocalPlayer.Character.Torso.CFrame.p).unit*60
    --game.Players.LocalPlayer.Character.Torso.Anchored = true
        for i = 1,13 do
        fireballs.Mesh.Scale = fireballs.Mesh.Scale +Vector3.new(0.5,0.5,0.5)
        wait()
    end
    bv2.Parent = fireballs
    fireballs.Anchored = false
    wait(2)
    --game.Players.LocalPlayer.Character.Torso.Anchored = false
    fireballs:Destroy()
    wait(1.5)
    cooldownFS = 1
    end
end
script.Parent.Activated:connect(shootFS)

Here's the server script:

wait()
local enabled = false
local re = game.Workspace.re
re.OnServerEvent:connect(function(name)
     Own = name
end)
print(Own)
function Damage(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")
    local t = hit.Parent:FindFirstChild("Torso")
    if t then
        script.Parent.CanCollide = false
    end
    print(Own)
    if h and not enabled and h.Parent.Parent.Name ~= Own then
        enabled = true
        for i = 1,10 do
            h.Health = h.Health - 2
            wait(1/15)
        end
    end
    enabled = false
    wait(3)
    game.Workspace.re:Destroy()
end
script.Parent.Touched:connect(Damage)

1 answer

Log in to vote
1
Answered by 8 years ago
RBXScriptSignal RemoteEvent.OnServerEvent ->
    Instance<Player> PlayerWhoFired,
    var ...

OnServerEvent fires with the Player who fired it as the first argument, and all the arguments as params 2+

This means that Own is a Player instead of a string, and you don't need to send the Player name. Own is also nil until you define it.

0
Ok, but I'm still not sure why I keep getting the error "19:03:40.936 - Workspace.Part.Damage:7: attempt to index global 'Own' (a nil value)" After the script is run a second time (When I shoot the projectile again). tre821 25 — 8y
0
^ Because on line 7 of the damage script, the script doesn't know what Own is until you define it running the function on line 4. M39a9am3R 3210 — 8y
Ad

Answer this question