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

Projectile's code is not executing when I clone it in a LocalScript?

Asked by 3 years ago

I'm pretty sure this is something I will have to remedy with a Remote Function or something but I'd thought I'd post my script here and see if someones points something out that I might have missed.

I'm not an absolute beginner to Roblox Lua, but this is the first time that I've ever decided to focus my knowledge on building something specific from scratch (barring the programming knowledge I can obtain off the Internet, free models, or even this site.)

I'm building a little Howitzer cannon that fires an explosive projectile when the F key is pressed. Here is the relevant code:

control = false -- default setting, if the local player has control over the howitzer or not
fired = false -- firing mechanism debounce

pilotseet:GetPropertyChangedSignal("Occupant"):connect(function ()
    wait(.5)
    if pilotseet.Occupant ~= nil and pilotseet.Occupant == humanoid then -- is seat is in use, and if user is localplayer
        control = true
        wait(.1)
        print("Artillery activated, user "..localplayer.Name.." operating cannon.")
        inputservice.InputBegan:connect(function (key, processed)
            if key.KeyCode == Enum.KeyCode.F and control == true and fired == false then
            fired = true
            local Howitzer_Shell = storage.HeavyShell -- gets the round
            local Emitted_Howitzer_Shell = Howitzer_Shell:clone() -- clones it for the purpose of emitting it
            Emitted_Howitzer_Shell.Name = ""..localplayer.Name.."'s HowitShell"
            Emitted_Howitzer_Shell.Parent = workspace
            Emitted_Howitzer_Shell.CFrame = cannonFunction.ArtilleryEmit.CFrame * CFrame.new(0, 0, 2)
            Emitted_Howitzer_Shell.Velocity = cannonFunction.ArtilleryEmit.CFrame.lookVector * 550
            local whistle = Emitted_Howitzer_Shell.shellScream
            whistle:Play()
            fireSound:Play() -- this code works fine, it makes the projectile come out the barrel as intended. i omitted the rest of the controls (traverse/elevation) but it all works despite not being connected to any remoteevents/remotefunctions
            end 
        end)

When the F key is pressed, the shell is properly cloned and emitted from the barrel, and when it touches something the following code SHOULD be executing. Don't go through it too deeply because it does work, I'll get into how after the code block

local explosionSound = sphere.explosionSound

function explosionReduce(explosionV)
    if explosionV ~= nil then
        explosionSound:Play()
        wait(.1)
        for i = 0, 1, 0.05 do
            explosionV.Transparency = i
            wait(.005)
        end
        explosionV.Transparency = 1
        explosionSound.Ended:wait()
        explosionV:Destroy()
    end
end

explosion = Instance.new("Explosion")
explosion.BlastRadius = 25
explosion.BlastPressure = 15
explosion.ExplosionType = Enum.ExplosionType.NoCraters
explosion.DestroyJointRadiusPercent = 1
explosion.Visible = false

function GetPosition(positionCoord)
    return positionCoord.Position
end

shell.Touched:connect(function (touch)
    if touch:IsA("BasePart") and touch.Name ~= "ExplosionOrb" then
        local impactPosition = GetPosition(shell)
        local activeBlast = explosion:clone()
        activeBlast.Name = "ImpactBlast"
        activeBlast.Position = impactPosition
        activeBlast.Parent = workspace
        if artillery_screech.Playing == true then
            artillery_screech:Stop()
        end
        shell.Parent = game.Lighting -- conveniently takes the shell out of the workspace so it stops generating touched events without having to delete it, which obliterates the script as well
        local FX = sphere:clone() -- custom blast effect defined but not in code block
        FX.Position = activeBlast.Position
        FX.Parent = workspace
        explosionReduce(FX) -- remove custom blast effect if it exists
        shell:Destroy()
    end
    end)

So the thing is, when the projectile is fired by the client, it simply falls through the map instead of its code executing. However, when the same method is applied for summoning the projectile (cloning it, setting its clone's position/velocity, etc.) in a while script in workspace, the projectile explodes perfectly fine when it is fired from the barrel.

So even though I suppose it's really obvious that I should be setting up some remote events/remote functions (especially if I want the cannon code to replicate across clients in the game I'm making), I want to know the best way of doing so. Should I let players control their own projectiles? Or the server? Would I have to do a major overhaul to my script if I were to relocate the code to a remote function (which in that case, where would I even put it?)

All help is appreciated, thanks!

0
Is the cannon ball set to CanCollide = true? DemGame 271 — 3y
0
I would try to use server as much as I can, as exploiters may get too much power if you leave it to the client Leamir 3138 — 3y
0
@DemGame It is not, but CanCollide doesn't have to be enabled for the Touched event to run. When I clone the projectile as is in the workspace, the clone's scripts run perfectly well and the projectile detonates where it is supposed to once it lands. JohnShotLincoln 0 — 3y
0
@Leamir You're very right, I'll keep that in mind. JohnShotLincoln 0 — 3y

Answer this question