I really need help..
I have this code for fly, as a backpack item:
Name = "Fly"
pi = 3.141592653589793238462643383279502884197163993751
a = 0
s = 0
ndist = 13
rs = 0.025
siz = Vector3.new(1, 1, 1)
form = 0
flow = {}
function CFC(P1,P2)
local Place0 = CFrame.new(P1.CFrame.x,P1.CFrame.y,P1.CFrame.z)
local Place1 = P2.Position
P1.Size = Vector3.new(P1.Size.x,P1.Size.y,(Place0.p - Place1).magnitude)
P1.CFrame = CFrame.new((Place0.p + Place1)/2,Place0.p)
end
function checktable(table, parentneeded)
local i
local t = {}
for i = 1, #table do
if table[i] ~= nil then
if string.lower(type(table[i])) == "userdata" then
if parentneeded == true then
if table[i].Parent ~= nil then
t[#t + 1] = table[i]
end
else
t[#t + 1] = table[i]
end
end
end
end
return t
end
if script.Parent.Name ~= Name then
User = game:service("Players").Nineza
HB = Instance.new("HopperBin")
HB.Name = Name
HB.Parent = User.StarterGear
script.Parent = HB
User.Character:BreakJoints()
end
speed = 50
script.Parent.Selected:connect(function(mar)
s = 1
torso = script.Parent.Parent.Parent.Character.Torso
LeftShoulder = torso["Left Shoulder"]
RightShoulder = torso["Right Shoulder"]
LeftHip = torso["Left Hip"]
RightHip = torso["Right Hip"]
human = script.Parent.Parent.Parent.Character.Humanoid
bv = Instance.new("BodyVelocity")
bv.maxForce = Vector3.new(0,math.huge,0)
bv.velocity = Vector3.new(0,0,0)
bv.Parent = torso
bg = Instance.new("BodyGyro")
bg.maxTorque = Vector3.new(0,0,0)
bg.Parent = torso
connection = mar.Button1Down:connect(function()
a = 1
bv.maxForce = Vector3.new(math.huge,math.huge,math.huge)
bg.maxTorque = Vector3.new(900000,900000,900000)
bg.cframe = CFrame.new(torso.Position,mar.hit.p) * CFrame.fromEulerAnglesXYZ(math.rad(-90),0,0)
bv.velocity = CFrame.new(torso.Position,mar.hit.p).lookVector * speed
moveconnect = mar.Move:connect(function()
bg.maxTorque = Vector3.new(900000,900000,900000)
bg.cframe = CFrame.new(torso.Position,mar.hit.p) * CFrame.fromEulerAnglesXYZ(math.rad(-90),0,0)
bv.velocity = CFrame.new(torso.Position,mar.hit.p).lookVector * speed
end)
upconnect = mar.Button1Up:connect(function()
a = 0
moveconnect:disconnect()
upconnect:disconnect()
bv.velocity = Vector3.new(0,0,0)
bv.maxForce = Vector3.new(0,math.huge,0)
torso.Velocity = Vector3.new(0,0,0)
bg.cframe = CFrame.new(torso.Position,torso.Position + Vector3.new(torso.CFrame.lookVector.x,0,torso.CFrame.lookVector.z))
wait(1)
end)
end)
while s == 1 do
wait(0.02)
flow = checktable(flow, true)
local i
for i = 1,#flow do
flow[i].Transparency = flow[i].Transparency + rs
if flow[i].Transparency >= 1 then flow[i]:remove() end
end
if a == 1 then
flow[#flow + 1] = Instance.new("Part")
local p = flow[#flow]
p.formFactor = form
p.Size = siz
p.Anchored = true
p.CanCollide = false
p.TopSurface = 0
p.BottomSurface = 0
if #flow - 1 > 0 then
local pr = flow[#flow - 1]
p.Position = torso.Position - torso.Velocity/ndist
CFC(p, pr)
else
p.CFrame = CFrame.new(torso.Position - torso.Velocity/ndist, torso.CFrame.lookVector)
end
p.BrickColor = BrickColor.new("Cyan")
p.Transparency = 1
p.Parent = torso
local marm = Instance.new("BlockMesh")
marm.Scale = Vector3.new(1.9, 0.9, 1.725)
marm.Parent = p
local amplitude
local frequency
amplitude = pi
desiredAngle = amplitude
RightShoulder.MaxVelocity = 0.4
LeftShoulder.MaxVelocity = 0.4
RightHip.MaxVelocity = pi/10
LeftHip.MaxVelocity = pi/10
RightShoulder.DesiredAngle = desiredAngle
LeftShoulder.DesiredAngle = -desiredAngle
RightHip.DesiredAngle = 0
LeftHip.DesiredAngle = 0
end
end
end)
script.Parent.Deselected:connect(function()
a = 0
s = 0
bv:remove()
bg:remove()
if connection ~= nil then
connection:disconnect()
end
if moveconnect ~= nil then
moveconnect:disconnect()
end
if upconnect ~= nil then
upconnect:disconnect()
end
while s == 0 do
wait()
if #flow > 0 then
flow = checktable(flow, true)
local i
for i = 1,#flow do
flow[i].Transparency = flow[i].Transparency + rs
if flow[i].Transparency >= 1 then flow[i]:remove() end
end
end
end
end)
while true do
wait()
if s == 1 then
return
end
end
script:remove()
The script is in a HopperBin Object in the game's StarterPack Folder.
Now if you try it on your own, you'll see that it will work, BUT if you publish the game, play it via ROBLOX(not the studio) and try to use the item, you won't fly.
Any ideas why?
It's been a long time since I haven't played ROBLOX, so my answer might be inaccurate.
First, you need to understand the difference between a Script and a Localscript. In a nutshell, a script runs server-side whereas a Localscript runs client-side. Your fly script is expected to run on a player's client, thus you have to use a Localscript.
Your regular script works in build-mode because scripts are run on your client, and thus are considered as Localscripts. When you publish your game and play it, your computer doesn't work as a server anymore, but as a client.
In addition to this, as you use a localscript, you may have to change the following line :
-- gets the client (local player) on which the script is running
User = Game:GetService("Players").LocalPlayer
Also, avoid relative paths such as script.Parent.Parent.Parent..... You want to get the client's torso, so just use your User variable as per : User.Character.Torso. Do the same for the character's Humanoid object.
I think it's because the time to load not existing try this at line1:
repeat wait() until game.Players.LocalPlayer.Character
Related
In games like phantom forces, or any FPS for that matter, if you look up or down, the arms and tools will stay on screen. In a new Roblox studio project, this does not happen by default. Basically I want the arms and tools to follow the camera’s rotation.
This can be done, but do you want other players to see the player turn the gun towards the camera?
local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Root = Character:WaitForChild("HumanoidRootPart")
local Neck = Character:WaitForChild("UpperTorso"):FindFirstChildOfClass("Motor6D")
local YOffset = Neck.C0.Y
local CFNew, CFAng = CFrame.new, CFrame.Angles
local asin = math.asin
game:GetService("RunService").RenderStepped:Connect(function()
local CameraDirection = Root.CFrame:toObjectSpace(Camera.CFrame).lookVector
if Neck then
if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
Neck.C0 = CFNew(0, YOffset, 0) * CFAng(0, -asin(CameraDirection.x), 0) * CFAng(asin(CameraDirection.y), 0, 0)
elseif Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
end
end
end)
This example only works with R15
If you don't want the players to see this, then create a model of the gun from the client's side and stick it on the camera
local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Root = Character:WaitForChild("HumanoidRootPart")
NAMES = {
screen_gun = "Local_Gun",
model= "Gun",
view = "view"
}
--- For Player
local Gun = {
screen = Instance.new("ScreenGui",Player:FindFirstChildOfClass("PlayerGui")),
obj=Instance.new("ViewportFrame",Player:FindFirstChildOfClass("PlayerGui"):WaitForChild("ScreenGui")),
part =Instance.new("Part",Player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("ViewportFrame")),
mesh = Instance.new("SpecialMesh",Player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("ViewportFrame"):WaitForChild("Part")),
offset = UDim2.new(0.7,0,0.6,0),
cam = Instance.new("Camera",Player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("ViewportFrame")),
offset2 = CFrame.new(Vector3.new(1,1,1),Vector3.new(0,0,0)),
size_view = UDim2.new(0,300,0,300)
}
Gun.obj.CurrentCamera=Gun.cam
Gun.part.Position = Vector3.new(0,0,0)
Gun.obj.Position = Gun.offset
Gun.obj.Size = Gun.size_view
Gun.obj.BackgroundTransparency = 1
Gun.cam.CFrame = Gun.offset2
Gun.screen.Name = NAMES.screen_gun
Gun.part.Name = NAMES.model
Gun.obj.Name = NAMES.view
Gun.part.Size = Vector3.new(1,1,2)
--
Gun.obj.Visible = false
local ToolInHand = false
Character.ChildAdded:Connect(function(obj)
if obj:IsA("Tool") and ( obj:FindFirstChildOfClass("Part") or obj:FindFirstChildOfClass("MeshPart") ) then --
if obj:FindFirstChildOfClass("MeshPart") then
obj:FindFirstChildOfClass("MeshPart").LocalTransparencyModifier = 1
Gun.mesh.MeshId = obj:FindFirstChildOfClass("MeshPart").MeshId
elseif obj:FindFirstChildOfClass("Part") then
obj:FindFirstChildOfClass("Part").LocalTransparencyModifier = 1
end
Gun.obj.Visible = true
ToolInHand = true
end
end)
Character.ChildRemoved:Connect(function(obj)
if obj:IsA("Tool") and ( obj:FindFirstChildOfClass("Part") or obj:FindFirstChildOfClass("MeshPart") ) then
if obj:FindFirstChildOfClass("MeshPart") then
obj:FindFirstChildOfClass("MeshPart").LocalTransparencyModifier = 0
elseif obj:FindFirstChildOfClass("Part") then
obj:FindFirstChildOfClass("Part").LocalTransparencyModifier = 0
end
Gun.obj.Visible = false
ToolInHand = false
end
end)
function CountCops()
local xPlayers = ESX.GetPlayers()
CopsConnected = 0
for i=1, #xPlayers, 1 do
local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
if xPlayer.job.name == 'police' then
CopsConnected = CopsConnected + 1
end
end
SetTimeout(120 * 1000, CountCops)
end
Does anyone know how would I have both if xPlayer.job.name == 'police' or xPlayer.job.name == 'sheriff' read for = 1 cop for each person.
So if one police was on then it = 1
and if one sheriff was on it would = 2.
local countingJobs = {
['police'] = true,
['sheriff'] = true
};
function CountCops()
local xPlayers = ESX.GetPlayers()
CopsConnected = 0
for i=1, #xPlayers, 1 do
local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
if countingJobs[xPlayer.job.name] then
CopsConnected = CopsConnected + 1
end
end
SetTimeout(120 * 1000, CountCops)
end
Hi I'm trying to make a flight script and I just need help with the ")" I honestly can't find where to put it. I know its a rookie mistake and I'm sorry for the waste of time. I have to over explain this and I'm sorry but I can't post any other way.
local camera = game.Workspace.CurrentCamera;
local character = game.Players.LocalPlayer.ChracaterAdded:Wait();
local F = game.Players.LocalPlayer.Backpack.Bold
local hrp = character:WaitForChild("HumanoidRootPart");
local humanoid = character:WaitForChild("Humanoid");
local animate = character:WaitForChild("Animate");
while (not character.Parent) do character.AncestryChanged:Wait(); end
local idleAnim = humanoid:LoadAnimation(script:WaitForChild("IdleAnim"));
local moveAnim = humanoid:LoadAnimation(script:WaitForChild("MoveAnim"));
local lastAnim = idleAnim;
local bodyGyro = Instance.new("BodyGyro");
bodyGyro.maxTorque = Vector3.new(1, 1, 1)*10^6;
bodyGyro.P = 10^6;
local bodyVel = Instance.new("BodyVelocity");
bodyVel.maxForce = Vector3.new(1, 1, 1)*10^6;
bodyVel.P = 10^4;
local isFlying = false;
local isJumping = false;
local movement = (forward = 0, backward = 0, right = 0, left = 0);
local FAD = F.Flying;
--------------Functions--------------
local function setFlying(flying)
isFlying = flying;
bodyGyro.Parent = isFlying and hrp or nil;
bodyVel.Parent = isFlying and hrp or nil;
bodyGyro.CFrame = hrp.CFrame;
bodyVel.Velocity = Vector3.new();
animate.Disabled = isFlying
if (isFlying) then
FAD.Transparency = 1
lastAnim = idleAnim;
lastAnim:Play();
else
FAD.Transparency = 0
lastAnim:Stop();
end
end
local function onUpdate(dt)
if (isFLying) then
local cf = camera.CFrame;
local direction = cf.rightVector*(movement.right - movement.left) + cf.lookVector*(movement.foward - movement.backward);
if (direction:Dot(direction) > 0) then
direction = direction.unit;
end
bodyGyro.CFrame = cf;
bodyVel.Velocity = direction * humanoid.WalkSpeed * 3
end
end
local function onJumpRequest()
if (not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead) then
return;
end
if (isFlying) then
setFlying(false);
isJumping = false;
else if (isJumping) then
wait(0.5)setFlying(true);
end
end
local function onStateChange(old, new)
if (new == Enum.HumanoidStateType.Landed) then
isJumping = false;
elseif (new == Enum.HumanoidStateType.Jumping) then
isJumping = true;
end
end
local function movementBind(actionName, inputState, inputObject)
if (inputState == Enum.UserInputState.Begin) then
movement[actionName] = 1;
elseif (inputState == Enum.UserInputState.End) then
movement[actionName] = 0;
end
if (isFlying) then
local isMoving = movement.right + movement.left + movement.foward + movement.backward > 0;
local nextAnim = isMoving and moveAnim or idleAnim;
if (nextAnim ~= lastAnim) then
lastAnim:Stop();
lastAnim = nextAnim;
lastAnim:Play();
end
end
return Enum.ContextActionResult.Pass;
end
-------------Connections-------------
humanoid.StateChanged:Connect(onStageChange);
game:GetService("UserInputService").JumpRequest:Connect(onJumpRequest);
game:GetService("ContextActionService"):BindAction("forward", movementBind, false, Enum.PlayerActions.CharacterFoward);
game:GetService("ContextActionService"):BindAction("backward", movementBind, false, Enum.PlayerActions.CharacterBackard);
game:GetService("ContextActionService"):BindAction("left", movementBind, false, Enum.PlayerActions.CharacterLeft);
game:GetService("ContextActionService"):BindAction("right", movementBind, false, Enum.PlayerActions.CharacterRight);
game:GetService("RunService").RenderStepped:Connect(onUpdate)
In line 18, movement's value should be a dictionary, rather than variables inside the parentheses.
local movement = {forward = 0, backward = 0, right = 0, left = 0}
I am making a simulator game on roblox and it has a pet system. I revamped how the hatch works, and for some reason it started to glitch out, and always give the same pet, and the pet was always the rarest one. I don't know what the problem is, here is the script. The Weight system worked perfectly before, but now it isnt working. I compared it to the old version of the game and the only difference is what the variables are called.
game.ReplicatedStorage.OpenEgg.OnServerInvoke = (function(player, amount, egg)
print("Fired")
if not player:FindFirstChild("Debounce") then
if amount == "One" then
local CC = player.leaderstats.CloudCoins
local EO = player.leaderstats.EggsOpened
local EGGS = require(game.ReplicatedStorage.EGGS)
local Data = EGGS[egg]
local Price = Data["Cost"]
local Pets = Data["Pets"]
local TotalWeight = 0
for i,v in pairs(Pets) do
TotalWeight = TotalWeight + v[1]
end
local function ChoosePet(player)
local Chance = math.random(1,TotalWeight)
local Counter = 0
for d,c in pairs(Pets)do
Counter = Counter + c[1]
if Chance >= Counter then
return d
end
end
end
local function GetChance(ChosenPet)
for i, v in pairs(Pets)do
for o,b in pairs(Pets) do
if o == ChosenPet then
return b[1]
end
end
end
end
if CC.Value >= Price then
local ChosenPet = ChoosePet(player)
local Chance = GetChance(ChosenPet)
CC.Value = CC.Value - Price
EO.Value = EO.Value + 1
local Pet = Instance.new("BoolValue")
Pet.Name = ChosenPet
Pet.Parent = player.Pets
if not player.PetsA:FindFirstChild(Pet.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
local Level = Instance.new("IntValue")
local Xp = Instance.new("IntValue")
local Number = Instance.new("IntValue")
Number.Name = "Number"
Number.Value = player.PetsA[Pet.Name.." Amount"].Value
Number.Parent = Pet
Level.Name = "Level"
Xp.Name = "XP"
Level.Value = 1
Xp.Value = 0
Level.Parent = Pet
Xp.Parent = Pet
return("Hatched")
else
return("NotEnoughCoins")
end
end
if amount == "Triple" then
local CC = player.leaderstats.CloudCoins
local EO = player.leaderstats.EggsOpened
local EGGS = require(game.ReplicatedStorage.EGGS)
local Data = EGGS[egg]
local Price = Data["Cost"]
local Pets = Data["Pets"]
local TotalWeight = 0
for i,v in pairs(Pets) do
TotalWeight = TotalWeight + v[1]
end
local function ChoosePet(player)
local Chance = math.random(1,TotalWeight)
local Counter = 0
for d,c in pairs(Pets)do
Counter = Counter + c[1]
if Chance >= Counter then
return d
end
end
end
local function GetChance(ChosenPet)
for i, v in pairs(Pets)do
for o,b in pairs(Pets) do
if o == ChosenPet then
return b[1]
end
end
end
end
if CC.Value >= Price*3 then
local ChosenPet1,ChosenPet2,ChosenPet3 = ChoosePet(player),ChoosePet(player),ChoosePet(player)
local Chance1,Chance2,Chance3 = GetChance(ChosenPet1),GetChance(ChosenPet2),GetChance(ChosenPet3)
CC.Value = CC.Value - Price*3
EO.Value = EO.Value + 3
local Pet1 = Instance.new("BoolValue")
Pet1.Name = ChosenPet1
Pet1.Parent = player.Pets
local Level1 = Instance.new("IntValue")
local Xp1 = Instance.new("IntValue")
local Number1 = Instance.new("IntValue")
Number1.Name = "Number"
if not player.PetsA:FindFirstChild(Pet1.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet1.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet1.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
Number1.Value = player.PetsA[Pet1.Name.." Amount"].Value
Number1.Parent = Pet1
Level1.Name = "Level"
Xp1.Name = "XP"
Level1.Value = 1
Xp1.Value = 0
Level1.Parent = Pet1
Xp1.Parent = Pet1
local Pet2 = Instance.new("BoolValue")
Pet2.Name = ChosenPet2
Pet2.Parent = player.Pets
local Level2 = Instance.new("IntValue")
local Xp2 = Instance.new("IntValue")
local Number2 = Instance.new("IntValue")
Number2.Name = "Number"
if not player.PetsA:FindFirstChild(Pet2.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet2.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet2.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
Number2.Value = player.PetsA[Pet2.Name.." Amount"].Value
Number2.Parent = Pet2
Level2.Name = "Level"
Xp2.Name = "XP"
Level2.Value = 1
Xp2.Value = 0
Level2.Parent = Pet2
Xp2.Parent = Pet2
local Pet3 = Instance.new("BoolValue")
Pet3.Name = ChosenPet3
Pet3.Parent = player.Pets
local Level3 = Instance.new("IntValue")
local Xp3 = Instance.new("IntValue")
local Number3 = Instance.new("IntValue")
Number3.Name = "Number"
if not player.PetsA:FindFirstChild(Pet3.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet3.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet3.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
Number3.Value = player.PetsA[Pet3.Name.." Amount"].Value
Number3.Parent = Pet3
Level3.Name = "Level"
Xp3.Name = "XP"
Level3.Value = 1
Xp3.Value = 0
Level3.Parent = Pet3
Xp3.Parent = Pet3
return("Hatched")
else
return("NotEnoughCoins")
end
end
end
end)
take a closer look at these lines of code.
local TotalWeight = 0
for i,v in pairs(Pets) do
TotalWeight = TotalWeight + v[1]
end
local function ChoosePet(player)
local Chance = math.random(1,TotalWeight)
local Counter = 0
for d,c in pairs(Pets)do
Counter = Counter + c[1]
if Chance >= Counter then
return d
end
end
end
the next time we see this function it is to assign the chosen pet to the player. I would suggest playing around with the Chance value. Set it to something crazy and see if you get another pet instead.
I have two zombie objects, one is Grunt and one is Runner.
My Grunt.lua file:
function InitGrunt()
grunt = {}
grunt.x = 0
grunt.y = 0
grunt.speed = 120
grunt.hitBox = (sprites.grunt:getHeight() + sprites.grunt:getWidth())/2
grunt.hit = false
gruntDefCD = 2
gruntCD = gruntDefCD
gruntMinCD = 0.4
gruntTimer = gruntCD
gruntTimerDecr = 0.8
end
function SpawnGrunt()
local side = math.random(1, 4)
--randomize spawn position
table.insert( zombies, grunt )
end
and my Runner.lua:
function InitRunner()
runner = {}
runner.x = 0
runner.y = 0
runner.speed = 240
runner.hitBox = (sprites.runner:getWidth() + sprites.runner:getHeight())/2
runner.hit = false
runnerDefCD = 4
runnerCD = runnerDefCD
runnerMinCD = 2
runnerTimer = runnerCD
runnerTimerDecr = 0.95
end
function SpawnRunner()
local side = math.random(1, 4)
--randomize spawn position
table.insert( zombies, runner )
end
So zombie table will have grunts and runners. How do I print both of them in Draw()?
I can draw one with:
function DrawGrunts()
for i, z in ipairs(zombies) do
love.graphics.draw(sprites.grunt, z.x, z.y, PlayerZombieAngle(z), nil, nil, sprites.grunt:getWidth()/2, sprites.grunt:getHeight()/2 )
end
end
But how do I draw both of them in one function, ideally?
Give the different entities a pointer to their sprite object:
runner = { }
runner.x = 0
runner.y = 0
runner.sprite = sprites.runner
-- Rest of runner definition
grunt = { }
grunt.x = 0
grunt.y = 0
grunt.sprite = sprites.grunt
-- Rest of grunt definition
And your draw function becomes:
for i, z in ipairs(zombies) do
love.graphics.draw(z.sprite, z.x, z.y, ...)
end