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

i made a script that spawns a bomb when you press Q, why arent the bombs working?

Asked by 1 year ago

ok. i made a script that lets you spawn bombs when you press q. the bomb is a sphere with a decal and a script and its located in replicated storage. heres the script inside the bomb:

wait(6)
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 100
explosion.BlastPressure = 3000
explosion.DestroyJointRadiusPercent = 0
explosion.Position = script.Parent.Position
explosion.Parent = workspace
script.Parent.CanCollide = false

the bomb works when you put it in workspace. now i need it so that you can spawn the bombs in front of you when you press "Q" heres the script:

local plr = game.Players.LocalPlayer


local Bomb = game.ReplicatedStorage.bomb
local UIS = game:GetService("UserInputService")
UIS.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        print("Working")
        local clonetear = Bomb:Clone()
        clonetear.Parent = game.Workspace
        clonetear.CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.lookVector * 5
    end
end)

the script does spawn the bombs, but the problem is that they dont explode nor lose colission like the script told then to. im still learning scripting so pls make your answers simple.

2 answers

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
1 year ago
Edited 1 year ago

the answer may not be simple, the reason why it doesn't work is because the script that spawns the bomb is a LocalScript, meaning only the person who created the bomb can see it. however, the script inside the bomb is a Script, not LocalScript, anything these scripts do is visible to everyone, however, these scripts must be created using a Script, they can't be created from LocalScript, this is to prevent hackers from creating scripts that can change game of other players.

the solution to this is to have a LocalScript tell a Script to :Clone the bomb instead of cloning it on LocalScript, this is how you can use RemoteEvents, create a RemoteEvent in ReplicatedStorage, then, create a Script in ServerScriptService and put this code in it:

local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local Bomb = game.ReplicatedStorage.Bomb

-- Whenever LocalScript does RemoteEvent:FireServer, this code is going
-- to run
RemoteEvent.OnServerEvent.Event:Connect(function(plr)
    print("Script: working", plr)

    local clonetear = Bomb:Clone()
    clonetear.CFrame = plr.Character.HumanoidRootPart.CFrame + + plr.Character.HumanoidRootPart.CFrame.LookVector * 5
    clonetear.Parent = game.Workspace
end)

then, in the LocalScript, whenever you press Q, run the RemoteEvent:FireServer thing:

local UIS = game:GetService("UserInputService")

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        print("LocalScript: working")
        RemoteEvent:FireServer()
    end
end)

change name of the remote event to anything you'd like, i would choose something like CreateBomb.

minor details:

  • :connect -> :Connect (:connect is old)

  • .lookVector -> LookVector (.lookVector is old)

  • set .Parent last, it's for small performance gain

you can read about remote events at https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events.

0
i did everything as you said, but nothing happens at all when i press q. pakancina 19 — 1y
0
@pakancina i had mistake in my code, forgot to type .Event on line 06 imKirda 4491 — 1y
Ad
Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

I do not know if this is the problem but... You should have defined the Parent at the start i believe like this wait(6) local explosion = Instance.new("Explosion") explosion.Parent = workspace explosion.Position = script.Parent.Position explosion.BlastRadius = 100 explosion.BlastPressure = 3000 explosion.DestroyJointRadiusPercent = 0

script.Parent.CanCollide = false

Answer this question