Hey Roblox devs. I’m trying to make a muzzle flash for a gun tool. What I have is a script that checks whether the player is clicking and holding the left mouse button, and if he/she is then the muzzle flash will only show when the player is holding down the mouse. I have a remote event to make sure that everybody in the server can see the muzzle flash.
I also have it so when the player “equips” the gun in the gun selection menu, the tool itself gets transfered from replicated storage to the player’s backpack. But, I learned that when I equip the gun it gets transfered to the player character in game.Workspace.[my username]. I thought that I had made a mistake in my script, but I didn’t…So when you equip a tool, is it supposed to automatically go to the player’s character?
Now, my real problem is the fact that every time I try to reference the tool, for example if my tool was named “pistol” and I wanted to access it from the player’s backpack (which is where I want it) it would say “‘pistol’ is not a valid member of player.Backpack.pistol.” So, I figured I would reference it like player.Character.pistol, or game.Workspace[player.Name].pistol, but neither of those worked.
I’ve been having this issue for a while, but I think now its just because of my lack of Lua experience. I would appreciate it if somebody could give me any sort of advice for something like this.
First off, yes. The tool gets transferred to the player’s character when equipped. Second of all, try using WaitForChild(). So for example; player.Backpack:WaitForChild(“pistol”).
I’ve tried that, but it just stops the entire script since its waiting for a child named “pistol.” It’s almost like pistol doesn’t exist, but it obviously does.
I just tried that. I have a discard button in the menu where you equip the gun, and when I selected it and discarded it it worked how I wanted it to but then it gave me this error: “The Parent property of Blackout is locked, current parent: NULL, new parent Backpack”
Hm, there is a problem here, when managing guis, it needs to be in a local script. Meaning that if you were going to clone the tool in ReplicatedStorage, and parent it to the player’s backpack then you would need a remote event.
I have multiple scripts. The script that sends the tool into the player backpack is in the starterGui for the gun selecting menu (which is a local script). The script that fires the server and checks if the player is currently holding down the left mouse button is located here: pistol.part.particleEmitter(the muzzle flash). The script that recieves the remote event is located in server script service
I already have a local script in the gui. Should I have a remote event for each and every gun or just one? Also, the remote event can store more than one data at once right? Like, if two players equip different guns at the EXACT same time, would the remote event be able to transfer the those two pieces of data to the server at the same time or would one override the other?
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local player = game.Players.LocalPlayer
local buttonPressed = ReplicatedStorage.ButtonPressed
local template = script.Parent
–In a server script
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local handGun = ReplicatedStorage:WaitForChild(“Tools”):WaitForChild(“Handgun”)
local buttonPressed = ReplicatedStorage.ButtonPressed
buttonPressed.OnServerEvent:Connect(function(player)
local playerCharacter = game.Workspace:FindFirstChild(player.Name)
if not player.Backpack:FindFirstChild("Handgun") and not playerCharacter:FindFirstChild("Handgun") then
local handGunClone = handGun:Clone()
handGunClone.Parent = player.Backpack
print(playerCharacter)
else
print("You already have a gun")
end
end)
It basically gives the player the gun if they don’t own it.
Also sorry for the way this may be formatted, i’m not much of a devforum pro
If you wanted to delete it then yes you would still need a remote event. You need a remote event because GUIs can only be accessed on the clients side, and deleting the tool from a local script would only delete on the client side of things. The player may not see the tool but the server still thinks the player has that tool due to filtering enabled. Filtering enabled helps to prevent hackers from messing with objects. I hope you could understand that a bit better, I’m not the best at explaining. However, this would be the code:
–local script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local discardTool = ReplicatedStorage:WaitForChild("DiscardTool")
local discardButton = script.Parent
discardButton.MouseButton1Click:Connect(function()
discardTool:FireServer(player)
end)
–Server Script
discardTool.OnServerEvent:Connect(function(player)
local playerCharacter = game.Workspace:FindFirstChild(player.Name)
if player.Backpack:FindFirstChild("Handgun") then
local handGun = player.Backpack:FindFirstChild("Handgun")
handGun:Destroy()
elseif playerCharacter:FindFirstChild("Handgun") then
local handGun = playerCharacter:FindFirstChild("Handgun")
handGun:Destroy()
end
end)
Btw, the server script code can be in the same script as the cloning event script. For the local script,
I advice you make it a child of the button you are using to discard or clone. So the server code for these two buttons can be in the same server script, but the local scripts that deal with firing the remote events should be children of their respective buttons. Thankfully, I quickly learned how to quote my code properly haha.