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

should I make my paths different in a normal script than a local script?

Asked by 6 years ago
Edited 6 years ago

I tried using a normal script instead of a local script but I keep getting errors like "this is not a valid member of model" my code was originally in a localscript but when i move it to a normal script I get the errors. my script is down.

local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()
local tool = script.Parent
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
tool.Activated:Connect(function()
    local hit = mouse.Hit
    Event:FireServer(Hit)
end)

should I get rid of the local in front of my variables?

0
Please explain more, like what path did you use on the normal script? Your questions might get moderated if you do not provide enough info. Providing a script also helps. Mayk728 855 — 6y
0
k TheBishNextDoor -10 — 6y

1 answer

Log in to vote
-1
Answered by 6 years ago

Try this, also sorry if there are some errors this was typed on the phone.

if script.Parent.Parent.ClassName ~= "Model" then
    local plr = script.Parent.Parent
elseif script.Parent.Parent.ClassName == "Model" then
    local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
end
local char = plr.Character
local mouse = plr:GetMouse()
local tool = script.Parent
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
tool.Activated:Connect(function()
    local hit = mouse.Hit
    Event:FireServer(hit)
end)

At line 1 you attempted to call LocalPlayer which can only be accessed by a local script. Now how I got to my edit of that line is simple, it checks if the tool is equipped via method of if the tool is a child of players or the character, depending on the situation the variable will be different. Also do not remove the "local" before each variable, its better on the script and is good practice. Also on your original line 8 you incorrectly notated "hit".Remember variables are case sensitive.

0
ClassName is read only, you would have to use:IsA(), so technically, for line1 through 5 you would do if not script.Parent.Parent:IsA("Model") then local plr = script.Parent.Parent elseif script.Parent.Parent:IsA("Model") then local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent) end saSlol2436 716 — 6y
0
he's not attempting to write to ClassName^ creeperhunter76 554 — 6y
Ad

Answer this question