How to make locally assigned clothes visible to all players? - lua

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.

Related

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

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.

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

How to arrange picture in lua like a grid?

I'm learning lua and I want to arrange my bubble picture with some specific x and y coordinates, here's my code so far, the value of my j and i is only incrementing by 1 instead of the +29, I know I'm lacking some knowledge so any help will be appreciated
local background = display.newImageRect("blueBackground.png",642, 1040)
background.x = display.contentCenterX
background.y = display.contentCenterY
local x = 15
local y=15
for i=15,25 do
for j=15, 25 do
local bubble = display.newImageRect("bubble.png", 23,23)
bubble.x = i
bubble.y = j
j = j + 29
print("j",j)
end
i = i + 29
print("i",i)
end
This should helps you.
From Lua documentation
The for statement has two variants: the numeric for and the
generic for.
A numeric for has the following syntax:
for var=exp1,exp2,exp3 do
something
end
That loop will execute something for each value of var from exp1
to exp2, using exp3 as the step to increment var. This third
expression is optional; when absent, Lua assumes one as the step
value. As typical examples of such loops, we have
for i=1,f(x) do print(i) end
for i=10,1,-1 do print(i) end
Use
for i=15, 29*10+15, 29 do
for j=15, 29*10+15, 29 do
local bubble = display.newImageRect("bubble.png", 23,23)
bubble.x = i
bubble.y = j
print("j",j)
end
print("i",i)
end
or
for i=0, 10 do
for j=0, 10 do
local bubble = display.newImageRect("bubble.png", 23,23)
bubble.x = 15 + i * 29
bubble.y = 15 + j * 29
...

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