ServerScriptService.Server.Room.Item:60: attempt to index nil with ‘Character’
This is the function in which the code resides:
It’s a module script in ServerScriptService.
function item.New(location, itemName)
local itemObject = workspace.Items:FindFirstChild(itemName)
if itemObject==nil then
return
end
local objectToDuplicate = (itemObject:IsA("Tool") and itemObject:FindFirstChild("Handle")) or itemObject
local newObject = objectToDuplicate:Clone()
newObject:PivotTo(location.WorldCFrame)
local partToWeldTo = if newObject:IsA("Model") then newObject.PrimaryPart else newObject
if partToWeldTo:IsA("BasePart") then
local weld = Instance.new("WeldConstraint")
weld.Part0 = partToWeldTo
weld.Part1 = location.Parent
weld.Parent = partToWeldTo
end
newObject.Parent = location
local prompt = Instance.new("ProximityPrompt")
prompt.ActionText = ""
prompt.MaxActivationDistance = 5
prompt.Parent = location
prompt.Style = Enum.ProximityPromptStyle.Custom
local plr = game.Players.LocalPlayer
local toolequipped = plr.Character:FindFirstChildOfClass("Tool")
-- error ^^^
if not toolequipped then
prompt.Enabled = true
end
prompt.Triggered:Connect(function(player)
item.Interact(player, prompt, newObject, itemName)
end)
end
return item
So if the player is holding a tool named “Flashlight” then the proximity prompt is disabled; however, if they’re not holding a tool or are holding a different tool, the proximity prompt is enabled.
I think the way the script is overall laid out makes this difficult to achieve. Normally, I would suggest that within the flashlight script itself, you would disable the proximity prompt upon the flashlight being equipped via the Equipped event and enabled the proximity prompt upon the flashlight being unequipped. However, you don’t have access to the proximity prompt in that script since it’s created in this script.
I think the next best option, if you don’t want to rewrite a bunch of code, is to simply add a check in the prompt.Triggered event. Within the event, there should be an if statement that checks to see if the flashlight is equipped. You have the player in this instance, so doing this shouldn’t be difficult. If the player does not have the flashlight equipped, then call the Interact method. If the player does, nothing will happen (or alternatively you can make a little message that tells them they cannot interact with the object while holding the flashlight). This isn’t exactly the same as what you’re requesting, because you can still see the proximity prompt while holding the flashlight, but this method will prevent players from being able to actually do anything with the prompt.
I see! Thanks for your help! Let me figure out how to go about writing this in code & I’ll let you know if I need some help (I’m quite new to scripting so I really have no idea what I’m doing).
prompt.Triggered:Connect(function(player, toolequipped)
local toolequipped = player.Character:FindFirstChild("Flashlight")
if not toolequipped then
item.Interact(player, prompt, newObject, itemName)
end
end)
I was thinking something like this. prompt.Triggered is an event, not a property, so you can’t set it to false. This script essentially just checks if the toolequipped variable exists. If it does not, then call the Interact method. If it does do nothing.