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

ReplicatedStorage.ModuleScript:4:attempt to index local 'player' (a nil value), doesn't work ingame?

The module script & script I made both work in studio, but not ingame (the script I presume because it can't call the module script because of the error in the module script).

Module script:

local module = {}

local player = game.Players:WaitForChild("LocalPlayer")
local leaderstats = player:WaitForChild("leaderstats")
module.giveChickenEaten = function(chicken, chickenIntValue, biteAmountInt)
	--2
	--1
	print("Calculating chicken eaten...")
	print(chickenIntValue)
	leaderstats["Chicken Eaten"].Value = leaderstats["Chicken Eaten"].Value + chickenIntValue
	biteAmountInt.Value = biteAmountInt.Value - 1
	print(biteAmountInt.Value)
	if biteAmountInt.Value == 0 then
		chicken:Destroy()
	end
end

return module

Script (located inside a tool)

local Tool = script.Parent;

enabled = true




function onActivated(player)
	if not enabled  then
		return
	end

	enabled = false
	Tool.GripForward = Vector3.new(0.675, -0.675, -0.3)
	Tool.GripPos = Vector3.new(0.4, -0.59, 1.1)
	Tool.GripRight = Vector3.new(0.212, -0.212, 0.954)
	Tool.GripUp = Vector3.new(0.707, 0.707, 0)

	wait(.8)
	
	local h = Tool.Parent:FindFirstChild("Humanoid")
	if (h ~= nil) then
		if (h.MaxHealth > h.Health + 1.6) then
			h.Health = h.Health + 1.6
		else	
			h.Health = h.MaxHealth
		end
	end

	Tool.GripForward = Vector3.new(-0.976,0,-0.217)
	Tool.GripPos = Vector3.new(0, -0.4, 0)
	Tool.GripRight = Vector3.new(0.217,0,-0.976)
	Tool.GripUp = Vector3.new(0,1,0)
	
	--where i want to activate the modulescript
	
	local module1 = require(game.ReplicatedStorage.ModuleScript) 
	module1.giveChickenEaten(script.Parent, script.Parent.coldFriedChickenValue.Value, 	
        script.Parent.biteAmount)


	enabled = true

end



script.Parent.Activated:connect(onActivated)


1 answer

Your script doesn't work because the module script does not access players. Instead pass player = game.Players.LocalPlayer to the module script.

-- Module Script Function
module.giveChickenEaten = function(player,chicken, chickenIntValue, biteAmountInt)
    print("Calculating chicken eaten...")
    print(chickenIntValue)
    leaderstats["Chicken Eaten"].Value = leaderstats["Chicken Eaten"].Value + chickenIntValue
    biteAmountInt.Value = biteAmountInt.Value - 1
    print(biteAmountInt.Value)
    if biteAmountInt.Value == 0 then
        chicken:Destroy()
    end
end
--Script located inside a tool 
local player = script.Parent.Parent
local module1 = require(game.ReplicatedStorage.ModuleScript) --where the module script is activated
module1.giveChickenEaten(player,script.Parent, script.Parent.coldFriedChickenValue.Value, script.Parent.biteAmount)

12 comments

0

The rest of the script won't work as a local script.

0

Convert it to filtering enabled using remoteEvents, what you were trying to do wouldn't work even if it could run from a module script since it's not a localscript.

0

However, you could also just fine the player by doing script.Parent.Parent

0

How would I activate the script after something happens (what happens is in a script, not localscript).

0

Do you mean the module script? If so call the function in the module script. If not, there are several ways. One way is to have it disabled then enable it with another script. Another way is to use remoteEvents and fireclient/fireserver. A final way is to use .Changed to see if a condition is changed and if that condition is changed then that portion of the script will execute.

0

I updated the question to show you what I mean

0

You already are activating it. Chances are it doesn't work in-game is because you're still using localplayer which only works with localscripts.

0

Yes, so how would I get around that?

0

Like I said, do local player = script.Parent.Parent I already edited my answer.

0

Alright, I put in the second part of code, but there's an error: 22:16:04.974 - Infinite yield possible on 'Workspace.Vid_eo:WaitForChild("leaderstats")'

0

That means that you are never creating "leaderstats"

0

Leaderstats isn’t in workspace, though

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