I want my script to be able to recognise the player and to work without issues.
I get an error when I use this variable outside of the PlayerAdded function.
Basically this script adds an attachment to a humanoid once a tool is equipped.
Above: Error in output
Above: player shown with only ONE tool instead of the TWO tools I wanted.
I read through multiple articles on the DevForum in regards of accessing a player from a server script, none have been exactly what I wanted. I really don’t want to use remoteevents, because I want to keep the amount of scripts I have minimal. I tried printing the player name, however it only works when I do it in the playeradded function.
local knife = game.ServerStorage["Dual Wield Long Sword"]
local players = game:GetService("Players")
local char
players.PlayerAdded:Connect(function(player)
plr = player
print(plr.Name)
player.CharacterAdded:Connect(function(character)
char = character
end)
end)
local tool = plr:WaitForChild("Backpack"):WaitForChild("Dual Wield Long Sword")
tool.Equipped:Connect(function()
char.Humanoid:AddAccessory(knife:Clone())
print("Should have been added sir")
end)
Forgot to add, I need this urgently, and sorry if I got anything wrong, this is my first post on the DevForum.
The console error shows “DualWieldLongSword” which has no spaces but your provided code references “Dual Wield Long Sword” twice, which does have spaces.
Found the fix for anyone wondering:
local knife = game.ServerStorage["Dual Wield Long Sword"]
local players = game:GetService("Players")
local char
plr = nil
players.PlayerAdded:Connect(function(player)
plr = player
print(plr.Name)
player.CharacterAdded:Connect(function(character)
char = character
local tool = plr:WaitForChild("Backpack"):WaitForChild("Dual Wield Long Sword")
tool.Equipped:Connect(function()
char.Humanoid:AddAccessory(knife:Clone())
print("Should have been added sir")
end)
end)
end)
Thanks for trying to help but that wasn’t it, roblox removes the spaces when errors are displayed in output for some reason. Thanks so much for trying though!
local knife = game.ServerStorage["Dual Wield Long Sword"]
local players = game:GetService("Players")
local char
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
char = character
local tool = player:WaitForChild("Backpack"):WaitForChild("Dual Wield Long Sword")
tool.Equipped:Connect(function()
char.Humanoid:AddAccessory(knife:Clone())
print("Should have been added sir")
end)
end)
end)
Improved on the efficiency slightly. I noticed the original error shortly after some inspection but you had already found it.
Thank you so much! (30 minimum)