I Was try to creating a GUI which when I click the button it will equip a flag, but the script doesn’t work.
Here the script
image.MouseButton1Click:Connect(function()
local tool = game.StarterPack:FindFirstChild("Flag")
if tool then
humanoid:EquipTool(tool)
end
end)
2 Likes
local Player = game.Players.LocalPlayer
image.MouseButton1Click:Connect(function()
local tool = Player.Backpack:FindFirstChild("Flag")
if tool then
humanoid:EquipTool(tool)
end
end)
I would suggest something like this
2 Likes
I Don’t know why but still not equipped
1 Like
Is humanoid
and image
defined?
there are multiple things wring with this.
local tool = game.StarterPack:FindFirstChild("Tool")
game.StarterPack
does not exist.
where are you defining humanoid
?
This should be your LocalScript instead:
--Services
local plrs = game:GetService("Players") --Getting the service 'Players'. It is good practice.
--Player variables
local plr = plrs.LocalPlayer --Get the LocalPlayer
local backpack = plr:WaitForChild("Backpack") --Get the LocalPlayer's Backpack
local char = plr.Character or plr.CharacterAdded:Wait() --Gets the Character
local humanoid = char:WaitForChild("Humanoid") --Gets the player's humanoid
--Core variables
local tool = backpack:WaitForChild("Flag") --Gets tool
local image = script.Parent --Assumes the LocalScript is inside the image button
--Core System
image.MouseButton1Click:Connect(function()
humanoid:EquipTool(tool)
end)
Make sure this is a LocalScript in the image button directly.
3 Likes
You’re using LocalPlayer on a Server script.
im pretty sure you can call LocalPlayer
using game:GetService("Players")
correct if I am wrong though
You can if it is a LocalScript, Server scripts can’t access the LocalPlayer.
alright. Thanks for letting me know. I guess we are getting into Remotes then lol
1 Like
I mean, I’m pretty sure that script is going to work with some changes if you switch it to a LocalScript.
Added WaitForChilds and CharacterAdded:Wait()
--Services
local plrs = game:GetService("Players") --Getting the service 'Players'. It is good practice.
--Player variables
local plr = plrs.LocalPlayer --Get the LocalPlayer
local backpack = plr:WaitForChild("Backpack") --Get the LocalPlayer's Backpack
local char = plr.Character or plr.CharacterAdded:Wait() --Gets the Character
local humanoid = char:WaitForChild("Humanoid") --Gets the player's humanoid
--Core variables
local tool = backpack:WaitForChild("Flag") --Gets tool
local image = script.Parent --Assumes the LocalScript is inside the image button
--Core System
image.MouseButton1Click:Connect(function()
humanoid:EquipTool(tool)
end
2 Likes
the tool will not work on the server due to FilteringEnabled.
1 Like
I’m pretty sure it will, I have used Humanoid:EquipTool(Tool) in many local scripts and they worked for everyone even though FE was enabled.
1 Like
Or you could try cross client-server communication with a remote event
LocalScript:
local RemoteEvent = game.ReplicatedStorage:WaitForChild("Remote Event Name Here") -- waits for the remote event object to be loaded
local image = script.Parent -- if the localscript is a child of the ImageButton (assuming it is)
image.MouseButton1Click:Connect(function()
RemoteEvent:FireServer() -- sends request to the server
end)
ServerScript:
local RemoteEvent = game.ReplicatedStorage:WaitForChild("Remote Event Name Here")
RemoteEvent.OnServerEvent:Connect(function(player)
local Backpack = player.Backpack
local Character = player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")
local Tool = Backpack:FindFirstChild("Flag")
if Tool then
Humanoid:EquipTool(Tool)
end
end)
2 Likes
Oh! Alright. Let me edit my code block then
Thanks for letting me know!
I was about to say that, but @Giggio450BR told me that LocalScripts can still equip tools with FE enabled. Anyway @evanrevan709, both mine and @HugeCoolboy2007’s solution ahould work.
2 Likes