Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why can't I access the Player in a module script from this setup?

Asked by 5 years ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.

When a player presses a key, it fires an event. The event runs whatever code is in the modulescript. But I always get the same error - about the Player, no matter how I go about it.

Client:

local key = "z"
local Mouse = game.Players.LocalPlayer:GetMouse()
local Player = game.Players.LocalPlayer
SpellDisableCheck = false
Mouse.KeyDown:Connect(function(Key)
    if Key:lower() == key and SpellDisableCheck == false then
        SpellDisableCheck = true
        game.ReplicatedStorage.Resources.MagicEvents.WaterSlicer:FireServer(Player, script.Damage)
        SpellDisableCheck = false
    end
end)

Module:

local WaterMagic = {}
function WaterMagic.Slicer(Player, damage)
    print("z pressed")
    local Animation = Instance.new("Animation")
    Animation.AnimationId = "http://www.roblox.com/asset/?id=357408908"
    local track = Player.Character.Humanoid:LoadAnimation(Animation)
    track:Play()
    local Torso = Player.Character.Torso
    local rand = {0, 15, -15, 45, -45}
    local Part = game.ReplicatedStorage.Resources.Parts:FindFirstChild("WaterSlicer")
    for i = 1, 10 do
        local b = Part:Clone()
        b.CFrame = Torso.CFrame * CFrame.new(math.random(-3, 3), math.random(0, 3), -3) * CFrame.Angles(180, math.rad(rand[math.random(1, #rand)]), 0)
        local f= Instance.new("StringValue", damage)
        f.Name = "Creator"
        f.Value = Player.Name
        local Damage = Instance.new("IntValue", damage)
        Damage.Name = "Damage"
        Damage.Value = 1 + (game.ReplicatedStorage.Resources.Remotes.Functions.GetPlayerStrength:InvokeServer() / 100)
        damage.Disabled = false
        local v = Instance.new("BodyVelocity", b)
        v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
        v.velocity = Torso.CFrame.lookVector * 125
        b.Anchored = false
        game:GetService("Debris"):AddItem(b, 4)
        b.Parent = workspace
        wait()
    end
    track:Stop()
end


return WaterMagic

The serverscript is basically creating a remotevent which requires the module script, and runs the respective function. Just incase I explained this bad, here is the serverscript.

local EventStore = game.ReplicatedStorage.Resources.MagicEvents
local Modules = game.ServerStorage.Magics
---Water Magic
local Water = require(Modules.Water)
local slicer = Instance.new("RemoteEvent", EventStore)
slicer.Name = "WaterSlicer"
slicer.OnServerEvent:Connect(function(Player, Damage)
    Water.Slicer()
end)
1
can we see what the output says? DragonSkyye 517 — 5y
1
can we see what the output says? DragonSkyye 517 — 5y
0
What does the error say? At which line in your code is it? Amiaa16 3227 — 5y
0
Line 6 of the modulescript - 'attempt to index player was nil' EvilMessor 9 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

You're not passing in the arguments to the ModuleScript. Look at line 8 in the server script. You simply call Water.Slicer() instead of Water.Slicer(Player, damage), which is exactly why the ModuleScript can't find a player object.

Ad

Answer this question