Roblox Studio Lua : Clone with local script - lua

I created a template or when someone touches that template, it's destroyed, but it's only for that person. I try to clone the model with a local script, but it does not work.
local part2 = script.Parent.MarioBrick:Clone()
part2.Parent = game.Workspace.Camera

I believe cloning the script's parent will also clone the script itself, and run the script again. Are you doing this intentionally? If not then it can cause strange side effects to happen.
Edit: Sorry, I misread your code.

Your code looks fine. I suspect that your problem is that your LocalScript is not in a place that is run by clients. If you want a LocalScript to run, it needs to exist on a Player's model somewhere. An easy way to do this is to add the LocalScript into StarterPlayer > StarterCharacterScripts
This will clone the contents into the character when they spawn. Here's my example that seems to work :
local testPart = Instance.new("Part")
testPart.BrickColor = BrickColor.Random()
testPart.Position = Vector3.new(math.random(-10, 10), 1, math.random(-10, 10))
testPart.Parent = game.Workspace.Camera
When I go into the Test tab, I can start a server with 3 players. Each of those 3 players will see a different color cube somewhere different.

Related

Script can't find tool in backpack

1. Summarize the problem
My script can't find the tool in the player backpack. The output of the print is always nil.
The toll is given by another script before the script that has to search it.
No error message appears.
2. Describe what you've tried
I've tried searching around but nothing fix my problem. I tried re-writing the code from 0 too.
3. Show some code
Simple script Code that clone the tool and place it in the backpack
script.Parent.Triggered:Connect(function(player)
local Frullato = game.ServerStorage.LemonadeStand.Tools.Frullato:Clone()
Frullato.Parent = player.Backpack
local Frappe = require(game.ServerScriptService.DummyMove)
end)
Simple script Code that can't find the tool. Print output is "nil"
script.Parent.Triggered:Connect(function(player)
local Smoothie = player.Backpack:GetChildren("Frullato")
print(Smoothie.name)
end)
Instead of :GetChildren() use :FindFirstChild(), and "Smoothie.name" should be replaced with "Smoothie.Name"

Roblox how can I search for a cloned item on the player

So I am trying to make a change color for my name GUI but I do not know how to get the cloned GUI item inside of the player. And the problem is I am new to RBLX studio and do not know how to start with this so anything would help ;)
If I understand the question correctly, you could clone the GUI from ReplicatedStorage and place it into the player's head. You could do this by putting these lines of code in a server-side script in ServerScriptService.
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- If you put the GUI inside of ServerStorage you could change the service to get ServerStorage
game.Players.PlayerAdded:Connect(function(player)
local playerTagClone = ReplicatedStorage:WaitForChild("NameOfYourGUI"):Clone() -- Replace "NameOfYourGUI" to the GUI inside of ReplicatedStorage
playerTagClone.Parent = game.Workspace:WaitForChild(player.Name).Head
end)
If you would like to search for an item you can use ItemParent:FindFirstChild("ItemName") -- Of course you would change the ItemParent and ItemName to what they say.
This code has not been tested, so let me know if you have any issues with the code or if you have any questions!

Start a local script from server on Roblox studio

I have a map which I would like to load in the workspace. The map also comes with its own local scripts. However, I don't know where to place them in the player. Maybe I can put them in StarterPlayerScripts and then start them from the server(if Roblox studio has such a functionality).
You can start the local script by simply changing his localscript.Disabled bool value, just change their disabled value to false inside properties when you create them. Example:
localscript.Disabled = true
--Disables localscript
--------------------------------------
localscript.Disabled = false
--Enables it
Check inside the LocalScript for any code statements of script.Parent and put the LocalScript inside whatever the Parent is.

It keeps showing the error, "Cloned is not a valid member of model"

So, what I'm trying to create is a simple sword fighting Roblox game, while following the tutorials of a youtuber called AlvinBlox. I was doing the tutorial and everything was going pretty well, until I tested the game in an actual local server with 2 people. The players load in perfectly, but they dont teleport to the desert map I made, which is in a Maps folder, inside ServerStorage. IT keeps showing the error, "Cloned is not a valid member of model". If you want more context for the code, its inside a while loop that always runs since the condition is set to "true". When I clicked on the error it directed me to line 4, or this line, "local ClonedMap = ChosenMap:Cloned()". Im not sure what the code wants, I dont even know what it means. Please help, thank you.
local AvailableMaps = MapsFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
Status.Value = ChosenMap.Name.." Chosen"
local ClonedMap = ChosenMap:Cloned()
ClonedMap.Parent = workspace
-- Teleport players to map
local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

Roblox Studio - Warning/Error when i try to sell the "snow" in my backpack

As you can see on the picture below I can't sell what I have in my backpack when I enter the circle. I get this error/warning and I can't figure out what the problem is. I have found out that the problem happens on line 5: player:WaitForChild
The Error/Warning on line 5: Infinite yield possible on Players.asbjornbonde.PlayerGui:WaitForChild("Stats")
Here is the picture:
Here is my code:
script.Parent.Touched:connect(function(Hit)
local player = game.Players:FindFirstChild(Hit.Parent.Name)
if player then
local leaderstats = player:FindFirstChild("leaderstats")
local PlayerGui = player:WaitForChild("PlayerGui"):WaitForChild("Stats").Backpack
if leaderstats and PlayerGui then
local Cash = leaderstats:FindFirstChild("Cash")
local snow = PlayerGui:FindFirstChild("snow")
if Cash and snow then
if snow.Value <= 0 then
else
Cash.Value = Cash.Value + 2 * snow.Value
snow.Value = 0
script.Parent.DigSound:Play()
script.Disabled = true
wait(0.1)
script.Disabled = false
end
end
end
end
end)
I have used many hours on trying to fix this problem but i really can't. I would appreciate help.
Infinite yield possible on is a warning in Roblox Studio that means there is a possibility that your script could be stuck on that line waiting forever if object it is waiting on doesn't exist and is never created.
As the only WaitForChild that is causing an error is two WaitForChild's joined together I'm guessing the script doesn't like you doing that.
First Solution
If you were to split both WaitForChild's into two different variables it shouldn't give you the error however this is creating another variable for no real reason so I would go for the second solution.
Second Solution
As the player has loaded in and touched the part it's safe to assume the GUI has loaded for them so you can change your WaitForChild to FindFirstChild if you still want to check you didn't get a nil or you can reference it normally: player.PlayerGui.Stats.Backpack
Hope this helps.
Think of WaitForChild() as a loop. It is constantly executing in the background, as fast as the Lua engine can execute it. Wouldn't recommend using this or any sort of un controlled loop in Roblox Lua as the engine doesn't handle it very well.
If this is a server script with a filtering enabled game, the issue is that the server cannot access existing members of PlayerGui. To work around this, you either need to run this code on the client (with the necessary changes), or you'll need to make use of a remote event!

Resources