I'll make this really simple. I'm making an admin commands system and I'm trying to turn on a TextButton inside of the player (plr.PlayerGui.adminUi) when they chat ":cmdb". I've debbuged with print statements and all works. I added a print statement to tell me the status of "commandbar.Visibility" and it says that it's true, even though you can't see it and the checkbox in properties is false.
This is what I've had so far, with no error messages. This works the first time, but never works again. (this is in a module script)
commands.cmdb = {0, function(plr, rsp)
game.Players[plr.Name].PlayerGui.adminUi.commandbar.Visible = true
end}
This is the explorer:
and this is the code for "Exit":
script.Parent.MouseButton1Up:Connect(function()
script.Parent.Parent.Visible = false
end)
Any help is appreciated, and if I missed anything neccessary, please let me know!
What you need to do is use remote events to connect the player to the server and you have to use a local script to change the visibility. :fireclient(player) is what you need to do from the server side
https://developer.roblox.com/en-us/api-reference/function/RemoteEvent/FireServer
Related
I have been making a Roblox simulator as a side project to learn how to make games, Now I have gotten some help as you can see below but it is still not working. I have got some pictures and videos in these links:
https://flickr.com/photos/195497771#N05
https://vimeo.com/user173767075
Currently my issue is still the title of my question and my tool not working. I changed it to a remote event and now it stopped changing my leaderstats. I have tried a bunch of different solutions but none work. My first script is my tool script. Here it is:
local player = game.Players.LocalPlayer
script.Parent.Activated:Connect(function()
if player.Debounce.Value == false then
game.ReplicatedStorage.Power:FireServer(script.Parent.Values)
local action = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animation)
action:Play()
end
end)
Now, that is in a LocalScript and that works. It prints that it activated and the animation plays. Now my problem is with a script in ServerScriptService not receiving the remote event. I have a print in it but nothing happens. Here is that script:
game.ReplicatedStorage.Power.OnServerEvent:Connct(function(player, valueFolder)
if player.Debounce.Value == false then
player.leaderstats.Sticks.Value += valueFolder.Power.Value
player.Debounce.Value = true
wait(valueFolder.Cooldown.Value)
player.Debounce.Value = false
end
end)
So, for my original question, the sell knows I touch it and it activates the event but nothing changes in the leaderstats. Here is my script to detect when the player touches the sell part.
local sellevent = game.ReplicatedStorage.Sell
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
print("sell fired")
sellevent:Fire()
end
end)
That script works. But that is a server script. Let me know if I should change it to a LocalScript. Now, here is my script that changes the leaderstats, it detects the the event fired but nothing changes in leaderstats.
game.ReplicatedStorage.Sell.Event:Connect(function(player)
print("bindable event fired")
if player ~= nil then
if player.leaderstats.Sticks.Value > 0 then
player.leaderstats.Cash.Value += player.leaderstats.Sticks.Value
player.leaderstats.Sticks.Value = 0
end
end
end)
I gave as much information as I could. If you need anything else I can get more pictures or videos but that is the best I could give. Any help appreciated!
Okay, after watching the video, I might have a hunch as to what's going wrong.
The code in your original question works just fine. If you were to print out the Sticks.Value before adding it to Cash.Value, you'd see that Sticks.Value stays at 0. And you were always adding 0 to Cash.
This is likely because you are using a Tool to increment Sticks.Value, and it is common for Tools to use LocalScripts to hook up logic. When you make changes to the world (or leaderstats values) in LocalScripts, those changes are only present on that client. They are not replicated up to the server. And since all of your cash logic is in server scripts, the Sticks value stays at 0 on the server.
So to fix this, you need to make sure that the code that increments Sticks.Value happens in a Script. And you can do that by using RemoteEvents in the same way you're using your BindableEvent, to communicate from the tool up to a server script.
So in your Tool's LocalScript you'd do something like this :
-- find the RemoteEvent saved in ReplicatedStorage
local stickEvent = game.ReplicatedStorage.GetSticksEvent
-- listen for when the Tool is used
script.Parent.Activated:Connect(function()
-- tell the server to give us some sticks...
stickEvent:FireServer()
end)
Then, in a server Script, listen for that RemoteEvent to fire to give the player some sticks :
-- find the RemoteEvent saved in ReplicatedStorage
local stickEvent = game.ReplicatedStorage.GetSticksEvent
-- listen for when the client tells us that they got some sticks
stickEvent.OnServerEvent:Connect(function(player)
-- give the player some sticks
player.leaderstats.Sticks.Value += 1
end)
Edit - in this most recent version, your Sell script has stopped working because the player argument is nil. In an earlier version of this code, the Touched script passed in the player, but you removed that code and broke the Sell script. So just rollback the script to its earlier state :
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.BindableEvents.Sell:Fire(player)
end
end)
I'm very new to Roblox studio and trying to get some basic functionality working. I am spawning some NPCs, and I would like to prevent them from climbing ladders. After reading documentation, it seems I should be able to do this by using Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false). I am inserting that code right after I create the NPC as follows:
local function spawnEnemy()
local enemy = ServerStorage.Enemies.Zombie:Clone()
enemy.Parent = workspace.Enemies
print("Setting climbing to false")
enemy.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
print(enemy.Humanoid:GetStateEnabled(Enum.HumanoidStateType.Climbing))
enemy.Humanoid.StateEnabledChanged:Connect(function()
print("state changed")
end)
end
The output as I start the game looks like:
Setting climbing to false
false
This is just what I would expect, and also note it does not output "state changed" so I know that no other part of the code is interfering.
However, this doesn't actually prevent the NPC from climbing, and in fact if I immediately type into the console (where it says "Run a command" at the bottom of roblox studio) this command:
print(workspace.Enemies.Zombie.Humanoid:GetStateEnabled(Enum.HumanoidStateType.Climbing))
The output is true.
Why doesn't the variable "stick"? Do I need to put this code somewhere else?
SetStateEnabled doesn't seem to replicate to the client. Your Run-a-command command executes against the client's workspace, and there it is still true.
If you put the same on the server (say add the following into a workspace script):
spawn(function()
while (true) do
print(workspace.Enemies.Zombie.Humanoid:GetStateEnabled(Enum.HumanoidStateType.Climbing))
wait(0.5)
end
end)
...you'll see that on the server, that property is in fact false.
Update:
to set it on the client, you can just hook up a handler on your Enemies folder, that will always set the Humanoid's climbing state for all your zombies:
workspace.Enemies.ChildAdded:Connect(function(child)
if (child.Name == "Zombie") then
child:WaitForChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
end
end)
just put that above into a LocalScript in StarterPlayerScripts.
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!
I have looked at a number of other questions and their answers but I still don't seem to be able to fix this error message. I am writing a script which allows one player to change the face of another player. Due to the new ROBLOX update it is FE compatible therefore I will put both the Local Script and Server Script below even though the error is in the Server Script.
Local Script:
plr = script.Parent.Parent.Parent.NameInput.Text
script.Parent.MouseButton1Click:Connect(function()
script.Parent.RemoteEvent:FireServer(plr)
end)
Server Script:
faceid = script.Parent.FaceID.Value
script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr)
script.Parent.PName.Value = plr
local plrname = script.Parent.PName.Value
print (plrname)
game.Players[script.Parent.PName.Value].Character.Face.Texture = faceid
end)
Hierarchy:
This is the image of the hierarch of the GUI I am creating
Error Message: This is the image of the error I am receiving when I press the 'Test Face' button which is named 'One' in the explorer.
The server script is a bit messy as I have tried a few different ways to get around this error so if you think I could change anything or add/remove anything I would appreciate feedback on that. However, the main issue currently is the error I am getting on Line 4. There was also a previous error on Line 7 which said 'bad argument #2 to '?' (string expected, got Object)', but I would like to solve this issue first. Help with the scripts in general would be appreciated if deemed necessary by the people who attempt to help me.
Thank you in advance,
Rohan
Instead of using, plr you should get the name of the plr by using plr.Name.This way, you'll be telling the system the name of the player instead of getting the object.
faceid = script.Parent.FaceID.Value
script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr)
script.Parent.PName.Value = plr.Name -- edit was here
local plrname = script.Parent.PName.Value
print (plrname)
game.Players[script.Parent.PName.Value].Character.Face.Texture = faceid
end)
local place = game.Players.LocalPlayer.PlayerGui
function inventrans(amount)
if place.inven.InvenSee:FindFirstChild("Lava")then
place.Lava.Amount.value = place.Amount.value+amount
script.Parent.Amount = script.Parentarent.Amount-amount
else
game.ReplicatedStorage.StorageIco.Lava:clone(1).Parent =
game.Players.LocalPlayer.PlayerGui.inven.InvenSee.Lava
place.Lava.Amount.value = place.Amount.value+amount
script.Parent.Amount = script.Parentarent.Amount-amount
end
end
inventrans(23)
I get the error Lava is not a valid member of Frame.
I am trying to make an inventory system for my game but there is an error that I do not know how to fix. please help
Make sure your script is a local script and located inside a tool or the player gui.
game.Players.LocalPlayer.PlayerGui will be nil if it's not the case.
Make sure you located the object correctly.
You should use :WaitForChild
Example: game.Workspace:WaitForChild("Part")
Which will wait for the part to be added in the Workspace. The reason why you should use it is because your code is ran so fast, the object doesn't have enough time to create the part, so it returns an error. But if you use WaitForChild, it'll wait for the object to load in and then it runs the code.