Attempt to index nil with 'value'

I tried to make it like if the player collects an item, the player can’t collect it again
but this is happen, can someone help?

20:25:18.339  Players.crucewtemenrizky.PlayerGui.Scripts.LocalScript:5: attempt to index nil with 'Value'  -  Client - LocalScript:5

local script

local Player = game.Players.LocalPlayer
local CollectEvent = game.ReplicatedStorage.CollectEvent

CollectEvent.OnClientEvent:Connect(function(pl, iscollected)
	if iscollected.Value == false then
		iscollected.Value = true
		Player.HeadsFound.Value +=1
	else
		print("HeadsFound")
	end
end)
local iscollected = script:WaitForChild("IsCollected")
local collectEvent = game.ReplicatedStorage.CollectEvent

script.Parent.Triggered:Connect(function(player)
	collectEvent:FireClient(player, iscollected)
end)

I recommend checking out: RemoteEvent | Documentation - Roblox Creator Hub

Are both of them local scripts?

i think the iscollected is inside ServerScriptService and since the client cant see in there, what they see is nil in place of iscollected

also, setting iscollected to true and checking it in a local script is a security risk, as a hacker could set it to false again, whereas if you set it to true and check it on the server they will have no access to it. this would also fix your bug

The receiver doesn’t take a player argument when it’s on the client. Remove “pl”

This is correct. The problem is in your client script, where there is the pl argument, but the server script is fine.

All you need to do is take that out.

For example:

Your original script:

local Player = game.Players.LocalPlayer
local CollectEvent = game.ReplicatedStorage.CollectEvent

CollectEvent.OnClientEvent:Connect(function(pl, iscollected)
	if iscollected.Value == false then
		iscollected.Value = true
		Player.HeadsFound.Value +=1
	else
		print("HeadsFound")
	end
end)

A fixed version:

local Player = game.Players.LocalPlayer
local CollectEvent = game.ReplicatedStorage.CollectEvent

CollectEvent.OnClientEvent:Connect(function(iscollected)
	if iscollected.Value == false then
		iscollected.Value = true
		Player.HeadsFound.Value +=1
	else
		print("HeadsFound")
	end
end)
3 Likes

they are local script and script

2 Likes

I was close without testing … I’ll just remove that junk.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.