The code seems to be ok, but only one part spawns? - lua

I tried to do a script that spawns parts from replicated storage every second but only one keeps spawning.
here is the script:
function PartSpawner1()
local partToClone = game.ReplicatedStorage.ore
local newPart = partToClone:Clone()
newPart.Parent = game.Workspace
newPart.Position = Vector3.new(math.random(-500, 500), math.random(1, 500), math.random(-500, 500))
end
while true do
PartSpawner1()
wait (1)
end
I tried changing the coordinates and nothing works. The code works fine without position specified.

Related

Running "RecomputeTargetPath()" causes Garry's Mod to crash

So I have been coding a nextbot in Gmod for a while now and have found a bug that causes Gmod to crash every time the function is run. here is the function:
function ENT:RecomputeTargetPath(path_target)
if self.testmode then
PrintMessage(HUD_PRINTTALK, 'recomputing target path')
end
self.path = Path("Chase")
if (CurTime() - self.LastPathingInfraction < 5) then
return
end
local targetPos = path_target:GetPos()
-- Run toward the position below the ENTity we're targetting, since we can't fly.
trace.start = targetPos
trace.filter = self:GetEnemy()
local tr = util.TraceEntity(trace, self:GetEnemy())
-- Of course, we sure that there IS a "below the target."
if (tr.Hit and util.IsInWorld(tr.HitPos)) then
targetPos = tr.HitPos
end
local rTime = SysTime()
self.path:Compute(self, targetPos)
if (SysTime() - rTime > 0.005) then
self.LastPathingInfraction = CurTime()
end
end
When this function is run, it recomputes the target path for the nextbot to make it move differently.
It's used in the context of an if that looks like this or something very close:
if (CurTime() - self.LastPathRecompute > 0.1) then
self.LastPathRecompute = CurTime()
self:RecomputeTargetPath(self:GetEnemy():GetPos())
end
As far as I know, all variables in the if are called before each.
As far as I know, all variables in the if are called before each.
I tried recalling them but that didn't work at all... I don't know what is happening.
Also, side note, the function gets called multiple times per second based on the player's movement.
I've also added debug prints to every line of the code to see where the issue was and the bug fixed itself when I added them but when I removed them the bug came back...
Any help?

how should I fix While True loop repeating and stopping unwantedly (Roblox)

I am trying to make a part duplicate then delete the duplication script with the new part without deleting the script in the old part, this is in Roblox studio.
This is my code
local block = script.Parent
while true do
local xrange = math.random(-250,250)
local zrange = math.random(-250,250)
local item = block:Clone()
item.Parent = game.Workspace
item.Position = Vector3.new(xrange,.5,zrange)
item["Block Script"]:Destroy()
game.ReplicatedStorage.ItemCount.Value = game.ReplicatedStorage.ItemCount.Value + 1
wait(1)
end
The problem is that it runs as if it is supposed to duplicate 3 times for every loop and rather than creating 1 new part each second, it creates 3 parts. The main issue is that sometimes 1 of the 3 new parts won't have the other script that I have inside the parts, making the part a useless part that causes problems.
Does anyone see a problem?
You can also comment if you want more clarification.
If this is just an error or cannot be solved through the information given, I can probably work around but please comment if you need my clarification
Its duplicating 3 times because you are destroying the script last.. what you should try doing is:
1- creating a new folder on workspace ( going to call it "blocks" for example )
2- making the block and script of the block as the children of the "blocks" folder
3- and placing this code on the script:
local block = game.workspace."**blocks**".(Your part name)
while true do
local xrange = math.random(-250,250)
local zrange = math.random(-250,250)
local item = block:Clone()
item.Parent = game.Workspace
item.Position = Vector3.new(xrange,.5,zrange)
game.ReplicatedStorage.ItemCount.Value = game.ReplicatedStorage.ItemCount.Value + 1
wait(1)
end
If this does not work you can reply to this comment.

The block is not being cloned and no error message is showing.(Roblox Studio)

I am making a placing system (it is basic so far) but it is not placing anything. the code works up until the 'while whenclicked = true' part, here's the code:
print('works')
while true do
print('works2')
local ImportedValueX = game.ReplicatedStorage.ActPosX
local ImportedValueY = game.ReplicatedStorage.ActPosY
local ImportedValueZ = game.ReplicatedStorage.ActPosZ
local Block = game.ReplicatedStorage.Experimental
local WhenClicked = game.ReplicatedStorage.WhenClicked.Value
print('works3')
while WhenClicked == true do
print('wore')
PlacedBlock = Block:Clone()
PlacedBlock.Parent = workspace
PlacedBlock.Position.X = ImportedValueX
PlacedBlock.Position.Y = ImportedValueY
PlacedBlock.Position.Z = ImportedValueZ
WhenClicked = false
wait(1)
end
wait(0.1)
end
The variables work fine and the whenclick part also works, i think the while whenclicked part is broken.
I see a problem here:
PlacedBlock.Position.X = ImportedValueX
PlacedBlock.Position.Y = ImportedValueY
PlacedBlock.Position.Z = ImportedValueZ
The X, Y, Z are read-only properties. You need to populate them by creating a new Vector3 object and assigning it to the Position property, like this:
PlacedBlock.Position = Vector3.new(ImportedValueX, ImportedValueY, ImportedValueZ)
Updated:
I am making the assumption that you are trying to use replicate storage to signal the mouse click state (whenClicked) from your client to the server. The server then checks on the state as well as the x/y/z position in a loop. This is not working, because ReplicatedStorage does not replicate your values to the server. That would probably be an opening for exploits otherwise.
So, in order to signal something from your client to your server you should use RemoteEvent or RemoteFunction (look those up in the reference manual). In your case, your server script could look something like this:
local event = Instance.new("RemoteEvent", game.ReplicatedStorage)
event.Name = "MyRemoteEvent"
local Block = game.ReplicatedStorage.Experimental
event.OnServerEvent:Connect(function(plr, position, whenClicked)
if whenClicked then
print('wore')
local placedBlock = Block:Clone()
placedBlock.Parent = workspace
placedBlock.Position = position
end
end)
So this would create a remote event in ReplicatedStorage and then listen to it. When it is called from the client, it will then do what you wanted to do (clone the part and position it).
In your client script you would trigger the event like this:
-- wait until the server has created the remote event
local event = game.ReplicatedStorage:WaitForChild("MyRemoteEvent")
-- do whatever you need to do, then call trigger the event:
event:FireServer(Vector3.new(5,5,5), true)

ReplicatedFirst:RemoveDefaultLoadingScreen() not firing in Studio

I'm back with yet another problem. I'm trying to make a custom loading screen for my game, but RemoveDefaultLoadingScreen doesn't seem to be firing. Can anyone help me with this? Here's my code:
local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
ReplicatedFirst:RemoveDefaultLoadingScreen()
local TweenService = game:GetService("TweenService")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
PlayerGui:SetTopbarTransparency(1)
local Loading = ReplicatedFirst.Load
local ScreenGui = Instance.new("ScreenGui")
ScreenGui.IgnoreGuiInset=true
local Bar = Loading.Frame.TextLabel
ScreenGui.Name = "LoadScreen"
Loading.Parent = ScreenGui
ScreenGui.Parent = PlayerGui
local Info = TweenInfo.new(0.7,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,true,0)
local Tween = TweenService:Create(Bar,Info,{Position=UDim2.new(0.8,0,0.6,0)})
spawn(function()
while true do
Tween:Play()
Tween.Completed:Wait()
end
end)
if not game:IsLoaded() then
game.Loaded:Wait()
end
for i=0,1,0.1 do
Loading.BackgroundTransparency=i
Bar.TextTransparency=i
Loading.Frame.Load.TextTransparency=i
wait()
end
ScreenGui:Destroy()
Everything works fine, except that the third line doesn't work in Studio. Is it supposed to be this way? Help would be greatly appreciated.
I tested it, it works just fine in Roblox Studio. Note this, though:
In studio the load screen goes away almost instantly, so you won't see much of a difference.
I'd put a wait(5) in your script before the if not game:IsLoaded() then line, that will simulate a 5 second load. Then you can play with having vs not having the ReplicatedFirst:RemoveDefaultLoadingScreen() command present, and you'll see the difference.

weird situation while using timer and movie clip in giderous studio

I'm currently using giderous studio to make a tower defense game. However the code doesn't run as expected.
I made a function that make the monster in the game move according to the route settled in a JSON file. The move process go well EXCEPT the first monster being spawned firet, I have absolutely no ideas why the monster stop on its way. (As shown as diagram)
The following is my code:
level.lua:
level = Core.class(Sprite)
function level:init(levelId)
local level = dataSaver.load("level");
--load attributes
self.lives=level[tostring(levelId)]["lives"];
self.route=level[tostring(levelId)]["route"];
self.round=level[tostring(levelId)]["rounds"];
self.towersAllowed=level[tostring(levelId)]["towers"];
--some varibles
self.currentRound=1;
self.monsterList={};
end
function level:spawnMonster()
local monsterType=self.round[1]["type"];
local spawnNumber=self.round[1]["number"];
--set timer for spawn
local timer = Timer.new(self.round[1]["spawnInterval"],0);--spawnNumber);
timer:start();
--spawn
timer:addEventListener(Event.TIMER,function()
local spawnedMonster = monster.new(monsterType);
spawnedMonster.instance:setPosition(self.route[1]["x"],self.route[1]["y"]);
self:moveMonster(spawnedMonster);
end);
--after spawn finished, wait for next round.
timer:addEventListener(Event.TIMER_COMPLETE,function()
local nextRound = Timer.new(self.round[1]["timeUntilNextRound"],1);
nextRound:start();
nextRound:addEventListener(Event.TIMER_COMPLETE,function()
print("nextRound");
end);
end);
end
function level:moveMonster(monsterToMove)
if (monsterToMove.currentDes<=#self.route) then
monsterToMove:move(self.route[monsterToMove.currentDes]["x"],self.route[monsterToMove.currentDes]["y"]);
monsterToMove:addEventListener("monsterMoveDone",function()
monsterToMove.currentDes=monsterToMove.currentDes+1;
self:moveMonster(monsterToMove);
end);
end
end
monster.lua:
monster = Core.class(Sprite)
function monster:init(type)
local monstersList = dataSaver.load("monster");
self.instance = quickImage(monstersList[type]["image"], _W/2, _H/2,monstersList[type]["width"], monstersList[type]["height"]);
self.speed=monstersList[type]["speed"];
self.health=monstersList[type]["health"];
stage:addChild(self.instance);
---some varibles...
self.currentDes=2
end
function monster:move(desX,desY)
local time=getDistance(math.abs(self.instance:getX()-desX),math.abs(self.instance:getY()-desY))/self.speed
local mc = MovieClip.new{
{1, time, self.instance, {x = {self.instance:getX(), desX, "linear"}}},
{1, time, self.instance, {y = {self.instance:getY(), desY, "linear"}}},
}
mc:addEventListener(Event.COMPLETE,function()
self:dispatchEvent(Event.new("monsterMoveDone"))
end);
end
from the code, quickImage(); is just a little function for making image sprite
Any ideas, I thought maybe i make the memory allocating wrong.
Special Note. I run Gideros Studio under OpenSUSE 12.2 with Wine, but i don't think its a matter because all stuff go well.

Resources