I apologize in advance for the script I'm going to present for this question, it's long.
Hi, I'm trying to get it so a cannon in my game will be able to shoot using a button in a GUI versus a physical button in the game.
The Script I tried using:
local gunBarrelOne = workspace.MediumBoat.cannonRR.GunBarrel.One local debounce = false local gunOne = true local cannonBall = Instance.new("Part") cannonBall.Size = Vector3.new(1,1,1) cannonBall.BrickColor = BrickColor.new(26) cannonBall.Shape = 0 cannonBall.BottomSurface = 0 cannonBall.TopSurface = 0 cannonBall.Name = "Cannon Shot" cannonBall.Elasticity = .1 cannonBall.Reflectance = 0 cannonBall.Friction = 0 game.StarterGui.seatgui.Frame.switch1.MouseButton1Click:connect(fire) function fire(player) local sound = workspace.MediumBoat.cannonRR.Switch:findFirstChild("GunSound") if sound == nil then sound = Instance.new("Sound") sound.Name = "GunSound" sound.SoundId = "http://www.roblox.com/asset?id=2101148" sound.Volume = 1 sound.Parent = workspace.MediumBoat.cannonRR.Switch end sound:play() local missile = Instance.new("Part") local barrel if gunOne == true then barrel = gunBarrelOne gunOne = true end local spawnPos = barrel.CFrame * Vector3.new(6, 0, 0) local dx = math.random(0,0) local dy = math.random(0,0) local dz = math.random(30,30) local mag = math.random(200,200) local v = barrel.CFrame:vectorToWorldSpace(Vector3.new(mag + dx,dy,dz)) local missile = cannonBall:clone() missile.Position = spawnPos missile.Velocity = v local new_script =workspace.MediumBoat.cannonRR.Switch.CannonBall:clone() new_script.Disabled = false new_script.Parent = missile local creator_tag = Instance.new("ObjectValue") creator_tag.Value = player creator_tag.Name = "creator" creator_tag.Parent = missile missile.Parent = game.Workspace end
SRRY FOR ANY TYPOS
Firstly you have used :connect
which is deprecated (use :Connect
).
Secondly, you have done this :
game.StarterGui.seatgui.Frame.switch1.MouseButton1Click:connect(fire)
However, before this line of code you haven't even made the function yet. Put this line of code after you've made the function.
Thirdly, with the MouseButton1Click
event, it doesn't give you the player who clicked(it gives you nothing). It would probably be better to use
local player = game:GetService("Players").LocalPlayer
or
local player = game.Players.LocalPlayer
to get the player.
Use this script and see if it works (cant guarantee that since, i haven't checked the whole code)
Also, make sure this is in a LocalScript
, and is in the button that is being clicked.
local gunBarrelOne = workspace.MediumBoat.cannonRR.GunBarrel.One local player = game:GetService("Players").LocalPlayer --gets the player local debounce = false local gunOne = true local cannonBall = Instance.new("Part") cannonBall.Size = Vector3.new(1,1,1) cannonBall.BrickColor = BrickColor.new(26) cannonBall.Shape = 0 cannonBall.BottomSurface = 0 cannonBall.TopSurface = 0 cannonBall.Name = "Cannon Shot" cannonBall.Elasticity = .1 cannonBall.Reflectance = 0 cannonBall.Friction = 0 function fire() local sound = workspace.MediumBoat.cannonRR.Switch:findFirstChild("GunSound") if sound == nil then sound = Instance.new("Sound") sound.Name = "GunSound" sound.SoundId = "http://www.roblox.com/asset?id=2101148" sound.Volume = 1 sound.Parent = workspace.MediumBoat.cannonRR.Switch end sound:play() local missile = Instance.new("Part") local barrel if gunOne == true then barrel = gunBarrelOne gunOne = true end local spawnPos = barrel.CFrame * Vector3.new(6, 0, 0) local dx = math.random(0,0) local dy = math.random(0,0) local dz = math.random(30,30) local mag = math.random(200,200) local v = barrel.CFrame:vectorToWorldSpace(Vector3.new(mag + dx,dy,dz)) local missile = cannonBall:clone() missile.Position = spawnPos missile.Velocity = v local new_script =workspace.MediumBoat.cannonRR.Switch.CannonBall:clone() new_script.Disabled = false new_script.Parent = missile local creator_tag = Instance.new("ObjectValue") creator_tag.Value = player creator_tag.Name = "creator" creator_tag.Parent = missile missile.Parent = game.Workspace end script.Parent.MouseButton1Click:Connect(fire) --connect the function to the event after we've made it
This is due to a common mistake. Some people use StarterGui to get the player's gui, right? WRONG, you need to do
game.Players.LocalPlayer.PlayerGui
Also, try to use non-deprecated stuff as deprecated stuff may get removed at any time without notice.
Hope this helps!