serverscriptservice script
game.ReplicatedStorage.AdminEvent.OnServerEvent:Connect(function(plr,action,arg1,arg2)
if action == "Teleport" then
plr.Character.HumanoidRootPart.CFrame = arg1.Character.HumanoidRootPart.CFrame
print(arg1)
print(arg1.Character.HumanoidRootPart.CFrame)
print(plr.Character.HumanoidRootPart.CFrame)
local gui button script
function OnClicked()
print("clicked")
local arg1
for index, v in pairs(game.Players:GetDescendants()) do if v:Is(script.Parent.Parent.Parent.TextLabel.TextLabel.Text) then arg1 = v
local action = "Teleport"
local plr = game.Players.LocalPlayer
game.ReplicatedStorage.AdminEvent:FireServer(plr,action,arg1)
end
script.Parent.MouseButton1Down:connect(OnClicked)
end
end
game.ReplicatedStorage.AdminEvent:FireServer(plr, action, arg1)
no errors in output or the stuff that I try printing like the CFrame
idk why i got logged in a different account when pressing login with google but I solved this
Related
I'm using the following script in a separate scene to keep my object "player"(it's a car) and load another scene.
DontDestroyOnLoad(transform.gameObject);
SceneManager.LoadSceneAsync(1);
but in my game, the state of car will be changed like its speed, indicator, the level of getting hit.
i wanna reset the status of this car to the original status when I click the button Restart Game.
Is there any way I can do to reset the car apart from destroying the car and switching back to the original scene to execute DontDestroyOnLoad again?
If I understood you right, you have public variables set to user defined values and you would like these to be the initial ones which was assigned by the user when you reload your initial scene. There are two solutions for this:
(a) Destroy the DontDestroyOnLoad gameObject so that when you reload the initial scene, a new instance of this will be created, hence your user defined values will be retained. So when you want to reload to initial scene from the end scene, create a script and add these:
Destroy (GameObject.Find("NameOfTheGameObject"));
SceneManager.LoadSceneAsync(1);
(b) Retain the car and its script, instead create another script and copy the initial values to the second script. For example, attach the below code to a new script and attach that script to your car gameObject:
Script 1 - DontDestroyOnLoad
public int Acceleration_Of_Car = 20;
public int Car_Force = 100;
public static ClassNameHere instance;
void Awake()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}else if(instance != this)
{
Destroy(gameObject);
}
}
Now create second script in the End Scene or even in the Initial Scene. Create variables that are the same as first script so that you do not get confused. Copy values from first script to second script, later apply values from second script to first script.
Script 2 - Copy and Apply
public int Acceleration_Of_Car;
public int Car_Force;
private Script1 script1; //Reference your first script here
public void Start()
{
script1 = (Script1) GameObject.FindObjectOfType(typeof(Script1)); //Call the first script
}
public void CopyValues()
{
Acceleration_Of_Car = script1.Acceleration_Of_Car;
Car_Force = script1.Car_Force;
}
public void ApplyValues()
{
script1.Acceleration_Of_Car = Acceleration_Of_Car;
script1.Car_Force = Car_Force;
}
I would say its better to use DontDestroyOnLoad() even for second script so that no other instance will be running :)
Destroy(GameObject.Find("GameObjectName"));
SceneManager.LoadSceneAsync(SceneIndex);
I have a multiplayer 2D game made in Unity and I wanted to put some onscreen buttons to be able to control it on the telephone. The problem is that when I put the Event Trigger on the button it uses the function from the script that has to use, but it can't open other functions called in the main one. In my code, I try to open the function TryFireBullet() whenever I am pressing the button and Debug.Log shows me that TryFireBullet() is opened, but the functions inside it are not opened at all. (when I press the button, Debug.Log() shows me "works", "works2" and no "works3"..)
Any ideas why it does not work and how to fix it?
Here is the problem part of the code:
public void TryFireBullet()
{
Debug.Log("works");
//if (ultimul_glont == 0 || Time.time > ultimul_glont + Delay_Bullet)
//{
ultimul_glont = Time.time;
Debug.Log("works2");
CmdFireBullet();
//}
}
[Command]
public void CmdFireBullet()
{
Debug.Log("works3");
GameObject bullet = Instantiate(bulletPrefab, gunTip.position, Quaternion.identity) as GameObject;
NetworkServer.Spawn(bullet);
RpcAdaugaViteza(bullet);
}
Here is the full code:
https://pastebin.com/WaWbQvTw
p.s. I can not use just a main function for my button because I need Server Commands
I'm making a spigot plugin (version 1.8.8) that has an function that I know works because it fires flawlessly through my command. However, when I call it at the end of a PlayerExpChangeEvent, it seems like vanilla leveling overrides the bar, making it go up way more that it is supposed to. Running the command/function after this happens makes the bar go back to how it is supposed to be. I've tried setting my event's priority to highest (and when that didn't work, to lowest) but no matter what my function appears to be completely ignored when called inside the event.
Here is some code:
#EventHandler(priority=EventPriority.HIGHEST)
public void onXpGain(PlayerExpChangeEvent event)
{
// Load custom levels from config
ArrayList<String> levelList = new ArrayList<String>(plugin.getConfig().getStringList("levels"));
if (!((String)levelList.get(0)).equals("none"))
{
Player player = event.getPlayer();
Iterator<String> var4 = levelList.iterator();
while (var4.hasNext())
{
String s = (String)var4.next();
String[] splits = s.split(" ");
int levelCompare = Integer.parseInt(splits[0]);
int playerLvl = player.getLevel();
// Detect if on correct tier, else continue iteration
if (playerLvl == levelCompare - 1)
{
// Calculate the player's new XP amount
int totalXp = player.getTotalExperience() + event.getAmount();
player.setTotalExperience(totalXp);
updateBar(event.getPlayer()); // <-- THIS IS THE FUNCTION
return;
}
}
// At max level
player.setTotalExperience(player.getTotalExperience() + event.getAmount());
player.setLevel(getHighestLevel(levelList));
player.setExp(1.0f);
}
}
And here is the function itself. Keep in mind that it works fine when called through a command and not an event. It's purpose is to use the player's total XP to set the level and bar. Neither set correctly in the event; it instead embraces vanilla leveling.
public static void updateBar(Player player) {
ArrayList<String> levelList = new ArrayList<String>(plugin.getConfig().getStringList("levels"));
int totalXp = player.getTotalExperience();
player.setLevel(getHighestLevelForXp(totalXp, levelList));
if (player.getLevel() < getHighestLevel(levelList)) {
int lvlDiff = getTotalXpToLevel(player.getLevel() + 1,levelList) - getTotalXpToLevel(player.getLevel(),levelList);
int xpDiff = totalXp - getTotalXpToLevel(player.getLevel(),levelList);
player.setExp((float)xpDiff/lvlDiff);
} else {
player.setLevel(getHighestLevel(levelList));
player.setExp(0.0f);
}
return;
}
The command where the function works correctly is a bare-bones call to the function and doesn't need a mention here. Does anyone know how to get my event to override vanilla xp gain? The update works through the command, just not natural xp gain. It is already confirmed that the event DOES fire, as the rest of the event changes the internal xp amount, but the visual effects are overridden by vanilla. Can anyone help? Thanks.
Only setting the Player's EXP won't be enough for your desired behaviour. The Vanilla behaviour will still complete, as you're not changing how the event will add EXP to the player.
Currently, your event is working like this:
And PlayerExpGainEvent isn't cancellable, so you cannot undo it's addition of EXP.
What you can do instead is to set the EXP the event will add to 0, therefore not changing the player's EXP after your interception.
event.setAmount(0); //Cancelling the EXP addition
I would recommend to set your event to a high priority, so that other events that depend on Experience gain won't trigger when you set the amount gained to 0.
I'm learning unity scripting. But I don't understand the following :
static private var lastRecalculation = -1;
static function RecalculateValue () {
if (lastRecalculation == Time.frameCount)
return;
ProcessData.AndDoSomeCalculations();
}
I don't get the third line or the condition in the IF statement. I know its a bit amateur, but pls help.
Thank You.
This is from the Time.frameCount documentation. This example shows how to create a function that executes only once per frame, regardless of how many objects you attach your script to. I suspect though that this example is incomplete because it never updates lastRecalculation (or it was assumed you would do so in AndDoSomeCalculations() ).
The reason setting lastRecalculation = -1 initially is so this function runs during the first frame.
Working version:
static var lastRecalculation = -1;
static function RecalculateValue () {
if (lastRecalculation == Time.frameCount) {
return;
}
lastRecalculation = Time.frameCount;
Debug.Log (Time.frameCount);
//ProcessData.AndDoSomeCalculations();
}
function Update () {
RecalculateValue();
}
Attach this script to 2 different GameObjects and run it. You will only see unique frame values 1,2,3,4,5.... even though 2 GameObjects are each calling RecalculateValue()
Now comment out the return and run it again. Now the ProcessData portion of that code runs for both objects every frame and you'll see something like 1,1,2,2,3,3,4,4......
I don't exactly understand what's the difference between tap and touch in Corona. I use both of them and when one touches on an object both listened for event until I wrote this piece of code which changes image when nextButton is touched. It's like when I touched the nextButton, It calls the function two times. However when I change it to tap, it worked smoothly. So can you tell me what is the difference between touch and tap and what was causing trouble when I use touch in this piece of code?
function nextButton:touch(event)
if i==7 then
else
i=i+1
imageObject:removeSelf()
imageObject =display.newImage(imageTable[i]..".jpg")
imageObject.x=_W/2
imageObject.y=_H/2
end
end
nextButton:addEventListener("touch", nextButton)
In the corona touch listeners you have 3 state:
function listener(event)
if event.phase == "began" then
-- in the 1st tap the phase will be "began"
elseif event.phase == "moved" then
-- after began phase when the listener will be called with moved phase,like touched coordinates
elseif event.phase == "ended" then
--when the touch will end
end
end
nextButton:addEventListener("touch", listener)
--[[
this was a simple touch listener for an image, self made button etc, btw when you need to yous buttons, use the ui library what is made exactly for this http://developer.coronalabs.com/code/enhanced-ui-library-uilua ]]
-- example for usage
local ui = require "ui" -- copy the ui.lua to your apps root directory
yourButton = ui.newButton{
defaultSrc = "menu/icon-back.png",--unpressed state image
x=85,
y=display.contentHeight-50,
defaultX = 110,
defaultY =80,
offset=-5,
overSrc = "menu/icon-back-2.png",--pressed state image
overX = 110,
overY = 80,
onEvent = buttonhandler,
id = "yourBtnId" -- what you want
}
local function buttonhandler(event)
if event.phase == "release" then
--if you have more buttons handle it whit they id-s
if event.id == "yourBtnId" then
-- when your button was finally released (like in 1st example ended, but this will be called only if the release target is your button)
end
end
end
A "tap" is a brief touch and release action. A touch can be a touch, move then release, or a touch and hold etc. A tap event simplifies your code because you only get one event: the tap happened. You don't have to code for all of the touch states.
A typical tap handler would look like:
local function tapHandler(event)
-- do stuff
return true
end
where has a touch handler that does the exact same thing would look like:
local function touchHandler(event)
if event.phase == "ended" then
-- do stuff
end
return true
end