Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

attempt to index function with 'Changed' error?

I have a bool value in workspace and I am trying to detect when that value is changed. I have a proximity prompt handler in server script service that when the proximity prompt is activated it changes the bool value to true. At the start of the script it creates the value, it's not already in workspace. There is then a local script in StarterGui that then detects when the value is changed and runs code based on that. I am getting this error: Players.electrocat56.PlayerGui.ProximityPromptDisabler:4: attempt to index function with 'Changed'

Proximity Prompt Handler Code

local ProximityPromptService = game:GetService('ProximityPromptService')

local value = Instance.new('BoolValue')
value.Parent = workspace
value.Name = 'Destroy'
value.Value = false

local function onPromptTriggered(promptObject, player)
	promptObject.Parent.Parent.Disabled = false
	player.Character.HumanoidRootPart.CFrame = CFrame.new(promptObject.Parent.Parent.Position)
	value.Value = true
end

ProximityPromptService.PromptTriggered:Connect(onPromptTriggered)

The Local Script

local boolValue = workspace.Destroy

wait(5)
boolValue.Changed:Connect(function(property)
	if property == 'Value' then
		if boolValue.Value == true then
			boolValue.Value = false
			game.Players.LocalPlayer.PlayerGui.ProximityPrompts:Destroy()
			print('Hidden Proximity Prompts')
		end
	end
end)

I have no idea why this is erroring as I have another script that uses a .Changed function that works perfectly.

1 answer

WorldRoot inherits the methods of Instance, one of which is :Destroy().

In your case, by referencing workspace.Destroy, the compiler believes you're attempting to allocate said method (function). To avoid this confusion, you can use :FindFirstChild(string Name) to ensure you're allocating an Instance, though, due to Client latency I suggest you call :WaitForChild() to simultaneously ensure your Instance has loaded.


##EDIT:

I suggest using :GetPropertyChangedSignal() to detect Property fluctuations:

BoolValue:GetPropertyChangedSignal("Value"):Connect()
    print(BoolValue.Value)
end)

1 comment

0

I've done this, and no longer get the error, but when the value changes, the script doesn't detect it

This discussion is preserved as it was. New answers, comments, and votes are closed.