(Roblox LUA Scripting) "Considering changing it to local"? - lua

I'm working on a creature-styled fighting game and have had some issues reusing tools from old Pokemon games. I've tried multiple things but in the end, the tools never do damage. They'll charge and/or shoot something, but it doesn't damage the other player. If I could get any help or ideas on what I can try, that'd be super helpful.
There's two different scripts involved, the tool script and the damage script, listed below.
Tool script:
01 bin = script.Parent
02 me = script.Parent.Parent.Parent
03
04 enabled = true
05
06 function onButton1Down(mouse)
07 if not enabled then
08 return
09 end
10
11 local player = game.Players.LocalPlayer
12 if player == nil then return end
13 enabled = false
14
15
16 mouse.Icon = "rbxasset://textures\\GunWaitCursor.png"
17
18 t = me.Character:findFirstChild("Torso")
19 if t ~= nil then
20
21 hax = game.Lighting.LeafBlade1:clone()
22 hax.Parent = t
23 wait(0.05)
24 p = Instance.new("Part")
25 p.Parent = game.Workspace
26 p.CanCollide = false
27 p.Transparency = 1
28 p.CFrame = me.Character.Torso.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 0)
29 d = Instance.new("BodyVelocity")
30 d.Parent = me.Character.Torso
31 d.maxForce = Vector3.new(math.huge, math.huge, math.huge)
32 d.velocity = p.CFrame.lookVector * 100
33 me.Character.Torso.CFrame = me.Character.Torso.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 0)
34 wait(0.15)
35
36 d:Remove()
37 p:Remove()
38 wait(0.1)
39 hax:Remove()
40
41 wait(3)
42 mouse.Icon = "rbxasset://textures\\GunCursor.png"
43 enabled = true
44
45 end
46 end
47
48
49 function onS(mouse)
50 mouse.Button1Down:connect(function() onButton1Down(mouse) end)
51 end
52 bin.Selected:connect(onS)
Damage script:
01 function onTouched(hit)
02 humanoid = hit.Parent.Parent:findFirstChild("Humanoid")
03 if humanoid ~= nil then
04 if humanoid.Parent ~= script.Parent.Parent then
05 humanoid.Health = humanoid.Health - 20
06 hit.CFrame = hit.CFrame * CFrame.fromEulerAnglesXYZ(-0.4, 0, 0)
07 for i = 1 , 1 do
08 p = Instance.new("Part")
09 p.Parent = game.Workspace
10 p.CanCollide = false
11 p.BrickColor = BrickColor.new(21)
12 p.Size = Vector3.new(1, 1, 1)
13 p.TopSurface = "Smooth"
14 p.BottomSurface = "Smooth"
15 p.CFrame = hit.CFrame
16 p.Velocity = Vector3.new(math.random(-50, 50), math.random(30, 50), math.random(-50, 50))
17 d = Instance.new("SpecialMesh")
18 d.Parent = p
19 d.MeshType = "Brick"
20 d.Scale = Vector3.new(0.2, 0.2, 0.2)
21 game:GetService("Debris"):AddItem(p,5)
22 end
23 end
24 end
25 end
26 script.Parent.Touched:connect(onTouched)

You are getting the “Considering changing it to local” error because none of the variables in the script have "local" in front of them. The script should function the same but you should still add "local" in front of those variables.
"bin = script.Parent" should be "local bin = script.Parent"

the problem is not that you need it to be local. you may need to create a secure damage remote for this. Because of the client/server boundary you will not be able to damage others serversidedly on the client.

Related

Lua Obfuscation - How would you make a lua string look like C++ compiled code

I was wondering, how would I make a simple lua string or entire code look look C++ compiled code but run as regular vanilla lua?
print("Test string") -- How would this look like C++ compiler code?
With Lua you can not directly dump print to a binary Format.
...as i know.
Dumping a Function to a Binary is easy doing with own defined Functions...
> -- Lua 5.4
> myfunc = function() print("Teststring") return end
> string.dump(myfunc, true)
uaT�
�
xV(w#����
��DGG��print�Teststring������
> load(string.dump(myfunc, true))()
Teststring
As you can see, like in a compiled C Binary the Constants are not obfuscated.
More obfuscating you can reach with converting the binary String to Bytecode...
> string.dump(myfunc, true):byte(1, -1)
27 76 117 97 84 0 25 147 13 10 26 10 4 8 8 120 86 0 0 0 0 0 0 0 00 0 0 40 119 64 1 128 129 129 0 0 2 133 11 0 0 0 131 128 0 0 68 0 21 71 0 1 0 71 0 1 0 130 4 134 112 114 105 110 116 4 139 84 101 115 116 115 116 114 105 110 103 129 0 0 0 128 128 128 128 128
...and for converting back later lets put it into a table...
> byte_code_tab = {string.dump(myfunc, true):byte(1, -1)}
> table.concat(byte_code_tab,',')
27,76,117,97,84,0,25,147,13,10,26,10,4,8,8,120,86,0,0,0,0,0,0,0,0,0,0,0,40,119,64,1,128,129,129,0,0,2,133,11,0,0,0,131,128,0,0,68,0,2,1,71,0,1,0,71,0,1,0,130,4,134,112,114,105,110,116,4,139,84,101,115,116,115,116,114,105,110,103,129,0,0,0,128,128,128,128,128
...now a function is needed to get it back...
> bytes_dec = function(tab) local txt = '' for k, v in pairs(tab) do txt = txt .. tostring(v):char() end return txt end
> bytes_dec(byte_code_tab)
uaT�
�
xV(w#����
��DGG��print�Teststring������
> load(bytes_dec(byte_code_tab))()
Teststring
EDIT
To show how it work with a single Lua file that returning a table with a __call metamethod check out this...
-- obfsc.lua
return setmetatable({27,76,117,97,84,0,25,147,13,10,26,10,4,8,8,120,86,0,0,0,0,0,0,0,0,0,0,0,40,119,64,1,128,129,129,0,0,2,133,11,0,0,0,131,128,0,0,68,0,2,1,71,0,1,0,71,0,1,0,130,4,134,112,114,105,110,116,4,139,84,101,115,116,115,116,114,105,110,103,129,0,0,0,128,128,128,128,128},
{__call = function(self, ...)
local txt = ''
for k, v in pairs(self) do
txt = txt .. tostring(v):char()
end
return load(txt)()
end})
...the bytes_dec function is stored in the __call metamethod...
$ /usr/local/bin/lua
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
> require('obfsc')
table: 0x565d3650 ./obfsc.lua
> require('obfsc')()
Teststring
...and do also the load()
But it is up to you where you store: bytes_dec()
Another nice method is ROT.
Its very simple and also old but good enough for de/obfuscating.
An Impression...
$ /bin/lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> rot=require('rot')
> -- Lets rotate the Banner
> print(rot('Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio'))
5!`unqnu``/092)'(4`hi`qyytmrpqr`
5!n/2'l`m)/ 51
> -- Now read source of rot.lua into rot_src and print it
> rot_src = io.open('rot.lua'):read('*a')
> print(rot_src)
-- rot.lua
local rotator = function(...)
local args, rot, c = {...}, {}, ''
for i = 1, 63 do rot[c.char(i)] = c.char(i + 64) end
for i = 64, 127 do rot[c.char(i)] = c.char(i - 64) end
return args[1]:gsub('.', rot)
end
return rotator
> -- Obfuscate the source and print it
> rot_obfsc = rot(rot_src)
> print(rot_obfsc)
mm`2/4n,5!J,/#!,`2/4!4/2`}`&5.#4)/.hnnniJ,/#!,`!2'3l`2/4l`#`}`;nnn=l`;=l`ggJJ&/2`)`}`ql`vs`$/`2/4#(!2h)i`}`#n#(!2h)`k`vti`%.$J&/2`)`}`vtl`qrw`$/`2/4#(!2h)i`}`#n#(!2h)`m`vti`%.$JJ2%452.`!2'3z'35"hgngl`2/4iJ%.$JJ2%452.`2/4!4/2J
> -- Deobfuscate and print on the fly
> print(rot(rot_obfsc))
-- rot.lua
local rotator = function(...)
local args, rot, c = {...}, {}, ''
for i = 1, 63 do rot[c.char(i)] = c.char(i + 64) end
for i = 64, 127 do rot[c.char(i)] = c.char(i - 64) end
return args[1]:gsub('.', rot)
end
return rotator
236

How to make locally assigned clothes visible to all players?

I have a local script that assigns shirts to players according to which team they are on. Here is the script:
view source
01 local player = game.Players.LocalPlayer
02
03 local char = player.Character or player.CharacterAdded:Wait()
04
05 local shirt
06
07 if player.Team == game.Teams["Red Team"] then
08 shirt = "http://www.roblox.com/asset?id=73022512"
09 elseif player.Team == game.Teams["Blue Team"] then
10 shirt = "rbxassetid://184244692"
11 elseif player.Team == game.Teams["Yellow Team"] then
12 shirt = "http://www.roblox.com/asset/?id=1210716332"
13 elseif player.Team == game.Teams["Green Team"] then
14 shirt = "http://www.roblox.com/asset/?id=13997666"
15 end
16
17 if char:FindFirstChild("Shirt") then
18 char.Shirt.ShirtTemplate = shirt
19 else
20 local newShirt = Instance.new("Shirt")
21 newShirt.Parent = char
22 newShirt.ShirtTemplate = shirt
23 end
This works. Problem is, other players can't see your shirt as it's only local. I can't paste this post on a server side script because - as far as I know - you can't access a character on a server side script. Am I right in assuming this? How would I make all shirts visible to all players? Thanks
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
-- here check the player's team and change the shirt
end)
end)
Try this way.

Lua Musical Notes to Numbers

I need to convert some musical note inputs representing a chord to numbers above it's root note 0 using Lua.
So from the midi data we get the notes of a C13 Chord
input: C, E, G, A#, D, F, A
as the root note 0 is C we start on the C note,
below we have 2 octaves of a piano keyboard, 12 notes on each where chords are played
0C 1C# 2D 3D# 4E 5F 6F# 7G 8G# 9A 10A# 11B 12C 13C# 14D 15D# 16E 17F 18F# 19G 20G# 21A 22A# 23B
so C is the root note 0
D,F,A are played on the next octave
result: 0,4,7,10,14,17,21
so if we have a D chord
input: D,F#,A
D is the root note 0
all notes played on the first octave
0D 1D# 2E 3F 4F# 5G 6G# 7A 8A# 9B 10C 11C# 12D 13D# 14E 15F 16F# 17G 18G# 19A 20A# 21B 22C 23C#
result: 0,4,7
G#m7#9 Chord
input: G#,B,D#,F#,B
0G# 1A 2A# 3B 4C 5C# 6D 7D# 8E 9F 10F# 11G 12G# 13A 14A# 15B 16C 17C# 18D 19D# 20E 21F 22F# 23G
result: 0,3,7,10,15
Something like this may work:
local function notes2nums(input)
local map = {A = 9, ["A#"] = 10, B = 11, C = 0, ["C#"] = 1, D = 2, ["D#"] = 3, E = 4, F = 5, ["F#"] = 6, G = 7, ["G#"] = 8}
local base, prev
return (input:gsub("([^,]+)", function(note)
local num = map[note] or error(("Unexpected note value '%s'"):format(note))
base = base or num
num = num - base
if prev and num < prev then num = num + 12 end
prev = num
return tostring(num)
end))
end
print(notes2nums("D,F#,A"))
print(notes2nums("C,E,G,A#,D,F,A"))
print(notes2nums("G#,B,D#,F#,B"))
This prints:
0,4,7
0,4,7,10,14,17,21
0,3,7,10,15

Lua table insert get same element

this is my code:
30 for t = 1,testData:size() do
33 -- get new sample
34 local input = testData.data[t]
35 if opt.type == 'double' then input = input:double()
36 elseif opt.type == 'cuda' then input = input:cuda() end
37 local target = testData.labels[t]
38 -- test sample
39 local pred = model:forward(input)
40 test_result[t]=pred
41
42 local err = criterion:forward(pred,target)
43 te_error = te_error+err
44 end
45 print(test_result[1])
46 print(test_result[2])
and I get the same elements, so my table only have stored the last element, why?
I wager that the model:forward(input) is returning a global table. So all testResult will point to the same global table. You can check this by printing pred after it is received: if a global table, it will always have the same "value" (pointer). Verify that model:forward returns a table local to that function.

Select Diagonal Elements of a Matrix in MATLAB

Consider the following matrix in MATLAB:
01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
I have to generate directional variograms for such 7 x 7 windows(moving) of an image. I will use nlfilter for the process but for developing the function to calculate variograms I am not able to decide how to select elements in the window. For example when I consider the central value 25, in EW direction I have to consider only 25, 26, 27 and 28; in NE direction I have to consider only 25, 19, 13 and 07 when the lag chosen is 1. Is there any standard command to do so?
You can also do it like this:
A = eye(5);
v = A(1:size(A,1)+1:end);
resulting in
v = [1 1 1 1 1]
You can write a function to get these elements yourself easily:
A = [01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49];
c = (size(A)+1)/2;
EW = A(c(1),c(2):end)
NE = diag(A(c(1):-1:1,c(2):end))
Just write this code in a function (preferably an m-file), perform your operation and pass the result back.
The diag function returns the diagonal elements of a matrix (or returns a diagonal matrix when passed a vector).
This is Generic matrix solution (not for MATLAB)
suppose matrix AxB =
[01 AA 03 04 05 06 07
08 09 AA 11 12 13 AA
AA 16 17 AA 19 AA 21
22 AA 24 25 AA 27 28
AA 30 AA 32 33 34 35
36 AA 38 AA 40 41 42
43 44 AA 46 AA 48 49];
in this matrix we want to search continuously 3 times appearence of AA diagonally.
Solution:-
step 1
for whole matrix we have to create 4 seperate for loops to search the appearence of AA continuously 3 times
i am adding method through which a user can search all loop and can find the item.
local function check_win( matrx_table)
local counter = 1
local term = "AA"
local D = 1
-- for horizontal check for win---
for i = 1, no_rows, 1 do
for j= 1, no_Columns, 1 do
if((j+1) <= no_Columns) then
if(table_mXn[i][j] == term and table_mXn[i][j+1] == term)then
counter = counter + 1;
else
counter = 1
end
if(counter == 3)then
return counter
end
end
end
end
counter = 1
-- for vertical check for win--
for i = 1, no_Columns, 1 do
for j= no_rows, 1, -1 do
if((j-1) >= 1) then
if(table_mXn[j][i] == term and table_mXn[j-1][i] == term)then
counter = counter + 1;
else
counter = 1
end
if(counter == 3)then
return counter
end
end
end
end
counter = 1
D = 1
-- for diagonol left half check for win in figure loop 1--
for m = 1, no_rows, 1 do
D = 1
for i =m, no_rows,1 do
if(i+1 <= no_rows and D+1 <= no_Columns)then
if(table_mXn[i][D] == term and table_mXn[i+1][D+1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D + 1
end
end
end
counter = 1
D = 1
-- for diagonol right half check for win in figure loop 2--
for m = 1, no_rows, 1 do
D = m
for i =1, no_rows,1 do
if(i+1 <= no_rows and D+1 <= no_Columns)then
if(table_mXn[i][D] == term and table_mXn[i+1][D+1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D + 1
end
end
end
counter = 1
D = 1
-- for diagonol left half check for win in figure loop 3--
for m = 1, no_rows, 1 do
D = no_Columns
for i =m, no_rows,1 do
if(i+1 <= no_rows and D-1 >= 1)then
if(table_mXn[i][D] == term and table_mXn[i+1][D-1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D - 1
end
end
end
counter = 1
D = 1
-- for diagonol left half check for win in figure loop 4--
for m = no_Columns, 1, -1 do
D = m
for i =1, no_rows,1 do
if(i+1 <= no_rows and D-1 >= 1)then
if(table_mXn[i][D] == term and table_mXn[i+1][D-1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D - 1
end
end
end
end
now you can call this method any where in class and can check in that matrix the searchable item is available or not in repeatedly order Horizontally, Vertically and diagonally.

Resources